0

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.

saurabh
  • 530
  • 1
  • 8
  • 22
  • I suspect the problem might be in making the ajax call to HTTPS URL. Are you sure that the call is reaching server side when ajax call is fired from iOS device? – Gandhi Jun 04 '16 at 05:40
  • @Gandhi thanks! but it must be reaching on server because success callback works in iPhone device. – saurabh Jun 04 '16 at 06:33
  • In that case how exactl you are throwing error from the server side to expect it to be caught in error callback? – Gandhi Jun 04 '16 at 06:36
  • @Gandhi I don't think I understood the question but I don't have the control over server , its cloud based backend as a service platform, it works fine in Android, browser and postman, this exact same code works in android and browser – saurabh Jun 04 '16 at 07:02
  • A 400 status code means that the request was malformed. In other words, the data stream sent by the client to the server didn't follow the rules. So i m sure you wont get either success or error callback in this case. – Gandhi Jun 04 '16 at 07:12
  • @Gandhi oops sorry my bad its 401 – saurabh Jun 04 '16 at 07:49

1 Answers1

1

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

Community
  • 1
  • 1
Gandhi
  • 11,875
  • 4
  • 39
  • 63