Make jQuery AJAX Run returned to Script

I have a link that I want to make a DELETE query using jQuery with AJAX.

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

resultis a piece of Javascript that I would like to run. How do I run it to return the returned script?

+3
source share
3 answers
success: function(result) {
    eval(result);
}
+12
source

Use option dataType: 'script'

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

Or simply,

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

Check out javascript Eval . This allows you to execute js code, which is presented as a string.

+3
source

All Articles