Monday, May 13, 2024
 Popular · Latest · Hot · Upcoming
78
rated 0 times [  84] [ 6]  / answers: 1 / hits: 6023  / 10 Years ago, thu, may 8, 2014, 12:00:00

I have the following logic within a function that needs to return a value based on the data returned back from an ajax call, however, I need the processing to happen after the success function.



var result;
console.log(First);
$.ajax ({
dataType: jsonp,
url: url,
async: false,
success: function(data) {
result = data;
console.log(Result: + result);
console.log(Second);
},
error: function(err) {
console.log(JSON.stringify(err))
}
});
console.log(Third);

console.log(Data: + result);


I expect to see the following in the console



First
Result: [object Object]
Second
Third
Data: [object Object]



However, I see the following in the console
First
Third
Data: undefined
Result: [object Object]
Second



It would seem like it's actually performing asynchronously or rather asynchronously right when it needs to call the success function.



I have tried making this not asynchronous by adding async: false. Is there a way to have the follow code after the ajax call execute AFTER the success function has executed. I want to return a boolean depending on the data return to the function that is housing this ajax call.



Looking at the forums I can find similar situations but they don't ask my specific question about executing the code after the ajax call AND the success function.


More From » jquery

 Answers
10

Data type jsonp does not support async:false



https://api.jquery.com/jQuery.ajax/
Cross-domain requests and dataType: jsonp requests do not support synchronous operation.



Should be easy to work around that now that you know.


[#45438] Wednesday, May 7, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kylanalis

Total Points: 438
Total Questions: 85
Total Answers: 102

Location: Barbados
Member since Sun, Nov 27, 2022
1 Year ago
kylanalis questions
Sat, Oct 2, 21, 00:00, 3 Years ago
Tue, Oct 13, 20, 00:00, 4 Years ago
Thu, Feb 13, 20, 00:00, 4 Years ago
Tue, Jan 7, 20, 00:00, 4 Years ago
;