I have a simple post, which executed the data returned as a script as such:
$.post("/home", { foo: 'bar' }, function(){
//callback function
}, "script");
Here’s what happens:
- The request gets sent
- A piece of JS code is returned as a
response and executed - The callback is executed
I need the callback to be executed before the returned JS is executed.
Is there a way to achieve this?
Thanks!
You can’t change how jQuery handle the request. After all, a callback is a callback and it gets executed once the job is done! You can, however, set the
dataTypeto"text"and then eval your Javascript.Ex:
** UPDATE **
This is what jQuery does internally (snipped) :
So, there is no security risk with taking the
globalEvalout of the Ajax request and executing it in the callback function.