Search code examples
ioscordovajquery-ajaxqcordova-5.0.0arrowdb

Ajax Error callback not firing for iPhone


Error callback does not work if async request is sent but success does! and it works perfectly fine in Android and browser. In iPhone it works for synchronous request. here is my code. other apis work perfectly fine.

$.ajax({
        type: 'POST',
        url: "https://api.cloud.appcelerator.com/v1/users/login.json?key=xxxxxxxxx",
        data: {
            "login": useremail,
            "password": password
        },
        success: function (resp) {                                               
            console.log(resp);
            console.log('User logged-in successfully');               
        },
        error: function (e) {
            console.log(e)
        }
    });

API returns status code 200 for correct email and password but 401 for incorrect one so if status code is 200 its works well I get response in success.


Solution

  • This seems to be a very common issue with Cordova + iOS + jQuery combo.

    Seems like there are few ways to resolve this 401 error response handling issue. One is to add timeout time out attribute while making AJAX request and handling the error. The other approach is to handle it in the server side by sending request over HTTPS and returning back authentication token with error details in case of 401 error.

    Request you to have a look at this post for more info.

    Also currently you cannot differentiate these two errors (401 and 408 errors) in iOS as i could see this defect still open in official Apache Cordova Bug Tracking System.Request you to check out this bug