在Ajax响应脚本评估 [英] Evaluating scripts in an ajax response

查看:79
本文介绍了在Ajax响应脚本评估的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的code:

$.ajax({
    type: 'POST',
    url: urlData,
    data: { OwnerId: ownerIdData, Text: textData },
    success: function (data) {
        $('#post-container').prepend(data);
    },
    error: function () {
    }
});

现在我想评估包含在成功的函数变量数据的脚本。
我该怎么办?
先谢谢了。

Now I want to eval the scripts contained in the variable data in the success function. How I do that ? Thanks in advance.

修改

我有以下形式:

<form class="new-post-form">

    <textarea id="post-creation-text-input" name="Text" rows="10"> Write something ... </textarea>
    <input type="hidden" value="@Model.OwnerId" id="post-creation-id-input"/>
    <input type="submit" value="Post" id="post-creation-submit-input" />


    <script type="text/javascript">
        $('#post-creation-submit-input').click(function (event) {
            event.preventDefault();
            var textData = $('#post-creation-text-input').val();
            var ownerIdData = $('#post-creation-id-input').val();
            var urlData = '@Url.Action("Create", "Posts")';


            $.ajax({
                type: 'POST',
                url: urlData,
                data: { OwnerId: ownerIdData, Text: textData },
                success: function (data) {

                    $('#post-container').prepend(data);

                    });
                },
                error: function () {
                }
            });

        });
    </script>

</form>

现在Ajax响应是以下几种观点:

Now the ajax response is the following view:

@using Facebook.Presentation.Web.Utils
@model Facebook.Presentation.Web.ViewModels.Posts.PostViewModel



<div class="post" id ="last-post">
    <h3>@Html.UserName(Model.Author)</h3>
    <br/>
    <div>
        @Html.DisplayFor(model => model.Text)
    </div>
    <br/>
    @{
        Html.RenderPartial("_CommentsPartial", Model.Comments, new ViewDataDictionary { { "ActionName", "Comment" }, { "ControllerName", "Posts" } });
    }  
</div>

这个响应也包含了必须进行评估的脚本。

This response also contains scripts that must be evaluated.

再次感谢。

推荐答案

使用 jQuery.getScript()功能。文档: http://api.jquery.com/jQuery.getScript/

这篇关于在Ajax响应脚本评估的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆