views:

39

answers:

1

Hello!

My problem would be that I make a POST request with jqurery, and if there is an error I modify the HTTP status to for example 401 and echo the error. (I'm using $.ajax();) So, the problem is that I don't know how to print out that error message.

If I check it in firebug I get something like this: {"*THE URL OF THE FILE*":{"rc":401,"body":"*error message*"}}

Can anybody tell me that how to print that error message out? (For example with alert();)

Thanks.

+3  A: 

I believe there is a difference between $.post and $.ajax... $.ajax includes both a success and error callback option where as $.post only has the success function. Read here for more info...

With that said, maybe the $.ajax would be a better fit to capture the error? Here is an example from the jQuery site on how this is done.

$.ajax({
          url: "script.php",
          global: false,
          type: "POST",
          data: ({id : this.getAttribute('id')}),
          dataType: "html",
          success: function(){
             //Happy Path...
          },
          error: function(msg){
             alert(msg.status);
          }
       };

Take a look at the "error: function(msg)"...

RSolberg
I've tried this one, and the response in this case is an object.
Tom
@Thomas: Launch firebug and look at the properties of it...
RSolberg
It contains the type of request, the url, data etc. But there is no response in it : /
Tom
That's right, the response is XMLHttpRequest object, and you can check HTTP-status using a corresponding property. It'd be msg.status, in terms of example above.
Oleksandr Bernatskyi
I mean message, sorry.
Tom
No problem, there is a property for that too. Try msg.statusText.
Oleksandr Bernatskyi
msg.responseText is that I was looking for. Thanks for your help. : )
Tom
You're welcome :)
Oleksandr Bernatskyi