Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Beginner
618 Views

Ajax Error callback not catching 401

Jump to solution

Hello,

I am trying to call my RESTful service, which works okey when i have access. The problem is that when ever i encounter a 401 status i would expect the code to go into the error handler attached to the ajax call. Its clear from the debug log that its getting the 401 response from the server however the xmlhttprequest object have status 200?

    var restcall= $.ajax({url: "https://test:7777/REST/service1",
                           type: 'post',
                           dataType: 'json',
                           data: {'username' : $('#username').val(), 'password': $('#pwd').val()},
                           headers:{'magic': '111'},
                           success: function( data) { 
                                            console.log(data);
                                        }                                        
                           }
                    })
    
  .done(function() {
    console.log( "done" );
  })
  .error(function(xhr, status, error) {
      console.log(xhr);
      console.log(status);
      console.log(error);
      alert(JSON.parse(xhr.responseText).message);
  });

I would expect the code to get into the error handler on 401, why does this not happen?

TIA

Tags (2)
0 Kudos

Accepted Solutions
Highlighted
Employee
618 Views

@Marc, jQuery has been notorious in handling error codes. It does not use error callback with jsonp datatype. See this post: http://forum.jquery.com/topic/jquery-ajax-with-datatype-jsonp-will-not-use-error-callback-if-request-fails

I would suggest you use statusText inside the success callback. xhr.status would return "Authorization Required" or "invalid user". See this post: http://stackoverflow.com/questions/2955947/how-do-i-get-the-http-status-code-with-jquery

View solution in original post

0 Kudos
5 Replies
Highlighted
618 Views
0 Kudos
Highlighted
Beginner
618 Views

I Paul F. Thanks for your quick response.

I read you link, however i am already using jquery 2.2.3.

Since i am slowly running out of ideas i gave the example in the link a shot. Unfortunately ending up with the same problem.

The result of running the code exmaple fitted to my service was:

POST https://test:7777/REST/service1 401 (Unauthorized)send @ jquery.js:9203jQuery.extend.ajax @ jquery.js:8684on_login @ app.js:223(anonymous function) @ app.js:31jQuery.event.dispatch @ jquery.js:4737elemData.handle @ jquery.js:4549
jquery.js:9203 XHR finished loading: POST "https://test:7777/REST/service1".send @ jquery.js:9203jQuery.extend.ajax @ jquery.js:8684on_login @ app.js:223(anonymous function) @ app.js:31jQuery.event.dispatch @ jquery.js:4737elemData.handle @ jquery.js:4549
app.js:233 jQuery version: 2.2.3
app.js:234 arg1: Object {message: "Invalid User"}
app.js:235 arg2: success
app.js:236 arg3: Object {readyState: 4, responseText: "{"message":"Invalid User/pass/servicekey"}", responseJSON: Object, status: 200, statusText: "success"}abort: ( statusText )always: ()complete: ()done: ()error: ()fail: ()getAllResponseHeaders: ()getResponseHeader: ( key )overrideMimeType: ( type )pipe: ( /* fnDone, fnFail, fnProgress */ )progress: ()promise: ( obj )readyState: 4responseJSON: ObjectresponseText: "{"message":"Invalid User"}"setRequestHeader: ( name, value )state: ()status: 200statusCode: ( map )statusText: "success"success: ()then: ( /* fnDone, fnFail, fnProgress */ )__proto__: Object
app.js:260 exit OK
app.js:261 {"message":"Invalid User"}

I dont know if the example output is helpful, but it clearly shows that the post returns 401 and the xmlhttprequest has 200. The error message that is returned from the service is shown. Removing this message makes the code throw parseerror because no json is returned. Any other suggestions would be appreciated, i would really like to be able to handle these 401. (I have tried changing the error to a 500 instead of 401 and same is happening so its not linked directly to the 401.)

 

0 Kudos
Highlighted
618 Views

Did you configure your whitelist settings on the Projects tab and add an appropriate CSP meta tag to your index.html file? See this link > https://software.intel.com/en-us/articles/cordova-whitelisting-with-intel-xdk-for-ajax-and-launching...

0 Kudos
Highlighted
Employee
619 Views

@Marc, jQuery has been notorious in handling error codes. It does not use error callback with jsonp datatype. See this post: http://forum.jquery.com/topic/jquery-ajax-with-datatype-jsonp-will-not-use-error-callback-if-request-fails

I would suggest you use statusText inside the success callback. xhr.status would return "Authorization Required" or "invalid user". See this post: http://stackoverflow.com/questions/2955947/how-do-i-get-the-http-status-code-with-jquery

View solution in original post

0 Kudos
Highlighted
Beginner
618 Views

Thanks SWATI S. pointing me in the right direction!

After studying the jsonp a bit more this is clear, i am now passing a custom json object on errors that include the error code and message. This way i can handle the errors inside the success callback function. Its not very pretty but it gets the job done.

Thx. 

0 Kudos