I have the following problem: there is no way to turn off asyncronous mode in $.ajax when using jsonp (http://api.jquery.com/jQuery.ajax/, async section). So the code
module = (function($)
{
var result = null;
var request = function(inputData)
{
$.ajax({
url: "http://some.site",
type: "post",
data: inputData,
dataType: "jsonp",
success: function(response)
{
result = response;
}
});
return result;
}
})($);
console.log(module.request());
will result in "null" on the first call, and in actual data on the second one. So how can I make javascript wait for ajax to complete request and only after that return the result?
It's not possible. You'll have to re-organize your code to work asynchronously. Here, i'll do it for you.
var module = (function($) {
var request = function(inputData) {
return $.ajax({
url: "http://some.site",
//type: "post",
data: inputData,
dataType: "jsonp"
});
}
return { request: request };
})($);
module.request().done(function(result){
console.log(result);
});
Also fixed a few other small mistakes, i assume those were just copy paste issues.