6

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.

Community
  • 1
  • 1
Mr_Green
  • 40,727
  • 45
  • 159
  • 271
  • How do you know it is not executing the `success` callback? FYI, `async: true` is the default setting, so I doubt that removing it causes any problems (or changes the behavior). – Felix Kling May 13 '13 at 07:38
  • 1
    what data does your server return ? try logging something in the console in the success callback, like console.log('success'); – palerdot May 13 '13 at 07:39
  • I checked keeping breakpoints inside and out of success and error callbacks in firefox – Mr_Green May 13 '13 at 07:40
  • You can change this `contentType: "application/x-www-form-urlencoded",` to this `contentType: "application/json",` – Jai May 13 '13 at 07:41
  • The `async` default is true and it's deprecated anyway so your call is asynchronous. You are probably trying to use the `sugData` array when it's still empty thus you think it's "not working". – Shadow The GPT Wizard May 13 '13 at 07:41
  • Remember that JSON syntax errors fail silently. Try creating a static JSON file to test with. Use http://jsonformatter.curiousconcept.com/ to check that it's formed correctly. – David Gilbertson May 13 '13 at 07:41
  • @palerdot It is going in success "console.log('success')" is working. but I am not getting the data. I am perplexed why breakpoints doesn't worked in this condition though. – Mr_Green May 13 '13 at 07:42
  • please give me sometime to update the issue. – Mr_Green May 13 '13 at 07:44
  • @Mr_Green: your success handler is not a problem then. I'm not aware of the exact data returned. You can format it in anyway you want if you know how and what data is returned by your server. Also check the .done() callback for ajax requests as it is preferred now. – palerdot May 13 '13 at 07:49
  • Just now I checked that the success calback function is happening and also all the arguments are passing the correct information. Though still I can't understand why my ajax call is showing null response. I am mentioning again, that the above ajax call was working fine when I was using `async: true`. – Mr_Green May 13 '13 at 07:50
  • @Mr_Green: you have to check out your server code then. The problem must be in there. – palerdot May 13 '13 at 07:50
  • see http://jsfiddle.net/2jtAC/1/. It is your ajax call in a working url that returns a json. It works just fine. Maybe your code works but it doesn't respons the right content. As of your data, you should return a json object. Try to add an alert in succes function to see if it goes there and then ensure that data argument in success function is a json – pbaris May 13 '13 at 07:51
  • If you are doing something with `sugData`, it should be within a function that is called from the `success` callback after populating `sugData`, nowhere else. – mccannf May 13 '13 at 07:52
  • @mccannf Ya understand your point. thanks I will try and let you know. – Mr_Green May 13 '13 at 08:49
  • @mccannf yup working fine :) thanks. thanks to all. – Mr_Green May 13 '13 at 08:52
  • possible duplicate of [How to return the response from an AJAX call?](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call) – Felix Kling May 13 '13 at 11:15

1 Answers1

5

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

Mr_Green
  • 40,727
  • 45
  • 159
  • 271