使JQuery AJAX Run返回脚本

Dre*_*rew 3 javascript ajax jquery

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

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

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

65F*_*f05 13

success: function(result) {
    eval(result);
}
Run Code Online (Sandbox Code Playgroud)

  • 在此用例中使用 eval() 是否存在任何风险? (2认同)

nat*_*lds 5

使用该dataType: 'script'选项

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

或简单地说,

$.getScript($(this).attr("href")); // Won't use 'DELETE' http type
Run Code Online (Sandbox Code Playgroud)