Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

neither success nor error is working in jquery ajax

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.

like image 267
Mr_Green Avatar asked May 13 '13 07:05

Mr_Green


Video Answer


1 Answers

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. :).

like image 51
Mr_Green Avatar answered Sep 28 '22 08:09

Mr_Green