使JQuery AJAX Run返回脚本

时间:2011-06-10 16:08:06

标签: javascript jquery ajax

我有一个链接,我想用JAX和AJAX发出DELETE请求。

if(confirm("Are you sure?")) {
   $.ajax({
    url: $(this).attr("href"),
    type: 'DELETE',
    success: function(result) {
            // Do something with the result
        }
    });
}

result是我想要运行的一大堆Javascript。如何让它运行我返回的脚本?

3 个答案:

答案 0 :(得分:13)

success: function(result) {
    eval(result);
}

答案 1 :(得分:5)

使用dataType: 'script'选项

$.ajax({
    url: $(this).attr("href"),
    type: 'DELETE',
    dataType: 'script'
});

或者简单地说,

$.getScript($(this).attr("href")); // Won't use 'DELETE' http type

答案 2 :(得分:2)

检查javascript Eval方法。它允许您执行表示为字符串的js代码。

相关问题