I am beginner to jquery ajax. My below ajax code was working fine when I was using async: false
, but due to problems in firefox I removed it after referring this link(I was facing the same issue). Now it is not working. not even showing any errors.
Here is my code:
try {
_ajaxCall = $.ajax({
url: URL,
type: "POST",
data: "drqlFragment=" + text,
//async : false,
cache: false,
headers: {
accept: "application/json",
contentType: "application/x-www-form-urlencoded"
},
contentType: "application/x-www-form-urlencoded",
//processData : true,
success: function (data, textStatus, jqXHR) {
var resData = data.suggestions;
for (var i = 0; i < resData.length; i++) {
sugData.push(resData[i].keyword);
}
},
error: function (response) {
//Error here
alert('hello');
}
});
} catch (e) {
alert(e);
}
The above code is neither executing success
nor error
. I even tried to catch the error by keeping try catch
block but no use.
The problem was that I was doing manipulations outside the success callback function. It was working fine when I used async: false
. that means the ajax call will be synchronous. When I removed async: false
, the manipulations which I was doing outside the success callback function were not working. The problem was because of asynchronous behaviour of the ajax call. When calling a ajax call asynchronously, it will not flow as the code priciple i.e step by step and top to bottom but it can happen anytime. So, I was not able to get the desired output.
When I replaced the code manipulations in success callback, my code is working fine. Thanks to @mccannf who pointed the problem in the above comments. :).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With