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?
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?
3 Answers
Reset to default 14success: function(result) {
eval(result);
}
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
Check the javascript Eval method. It allows you to execute js code which is represented as a string.
eval
, just return some kind of identifier and let your Javascript decide what to do. – kapa Commented Jun 10, 2011 at 16:10eval
should be avoided whenever possible. I don't think there is a real reason for returning a piece of code. You could return some kind of JSON, and define in your Javascript what should happen to it. – kapa Commented Jun 10, 2011 at 16:14