Drew
Drew

Reputation: 15408

Make JQuery AJAX Run returned Script

I have a link, which I want to make a DELETE request with JQuery with AJAX.

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

result is a wad of Javascript I would like to run. How do I get it to run my returned script?

Upvotes: 4

Views: 6161

Answers (3)

natedavisolds
natedavisolds

Reputation: 4295

Use the dataType: 'script' option

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

Or simply,

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

Upvotes: 5

Chris
Chris

Reputation: 7359

Check the javascript Eval method. It allows you to execute js code which is represented as a string.

Upvotes: 2

65Fbef05
65Fbef05

Reputation: 4522

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

Upvotes: 14

Related Questions