1

I understand that my issue is that jQuery is trying to parse the body of the response as json, but the body is undefined, and thus throws an error.

I cannot change the response. This is the default response from Jenkins servers. It sends a 201, 404, or 500 in the header, which I would like to handle.

my ajax:

 $(document).ready(function () {
    $('#reviewForm').bootstrapValidator({
      ...stuff...
      ...validation...
  }) 

 .on('success.form.bv', function (e) {
// Prevent form submission
e.preventDefault();

// Get the form instance
var $form = $(e.target);

// Use Ajax to submit form data
$.ajax({
    type: 'POST',
    url: url+$form.serialize(),
    dataType: 'text',
    complete: function() {
        alert("Success.");
    },
    error: function(xhr, status, text) {
        alert("failure");
    }
});

Despite a successful post (201 created), it will still hit error because of the syntax error due to the undefined body.

I would gladly handle the errors in the error: part of ajax, but I cant for the life of me figure out how to get the status code out of the header of the response.

And like I said, I would change the response if I could, but its just how Jenkins works.

Thank you.

EDIT: response header

Status Code: 201 Created
Connection: Keep-Alive
Content-Type: text/plain; charset=UTF-8
Date: Wed, 01 Oct 2014 14:51:12 GMT
Keep-Alive: timeout=15, max=100
Location: https://jenkins....
Server: Jetty(8.y.z-SNAPSHOT)
Transfer-Encoding: chunked

and this is the xhr (xml http response)

{
"readyState": 0,
"status": 0,
"statusText": "error"
}
Tyler Gregory
  • 33
  • 1
  • 8
  • What makes you think jQuery is trying to parse the response as JSON? According to your code, you're expecting `text` - so an `undefined` response body won't make a difference. – Adam Jenkins Oct 01 '14 at 15:07
  • 1
    `dataType: 'text'` means it won't parse the response at all. It'll leave it as a simple string. Whatever your error is, is coming from something else. – Spokey Oct 01 '14 at 15:10
  • I know that jquery handles it as an error because my failure alert pops up every time. I had a success alter in there but it would never pop. If it isnt a parse error, then it is something else, ill continue to investigate – Tyler Gregory Oct 01 '14 at 15:13
  • in your code in your post, you should be seeing both "failure" and "success" alerts, as the `complete` callback will run after the `error` one. – James Thorpe Oct 01 '14 at 15:15
  • @JamesThorpe yes i get those alerts. – Tyler Gregory Oct 01 '14 at 15:24
  • Are you sure you are seeing those response headers being returned and giving you an `xhr` `status` of `0`.? – Adam Jenkins Oct 01 '14 at 15:25
  • Perhaps [this is related](http://stackoverflow.com/questions/2000609/jquery-ajax-status-code-0) - If you're doing a query to jenkins, I'm guessing you're not on the same domain? – James Thorpe Oct 01 '14 at 15:29
  • uh. well. thats a good question. Yes im on the same domain. I can log into jenkins and see that the job is being kicked off successfully. – Tyler Gregory Oct 01 '14 at 17:37
  • @Adam yea, i do alert(JSON.serialize(xhr, null, 4)); and thats how it comes back – Tyler Gregory Oct 01 '14 at 17:50

5 Answers5

1

You can have specific callbacks for status codes such as:

$.ajax({
    ...
   statusCode: {
     201: function() { /* I received a 201 */ },
     404: function() { /* I received a 404 */ },
     500: function() { /* I received a 500 */ }
   }
});

This is listed in the documentation

Adam Jenkins
  • 51,445
  • 11
  • 72
  • 100
0

The status code determines which of the functions (i.e. complete ,error, or success ) that jQuery will fire.

So, try this:

$.ajax({
    type: 'POST',
    url: url,
    dataType: 'text',
    complete: function(response) {
        console.log(response); 
    }); 

In the future, this will allow you to debug a bit better. (via your inspect element console)

But, the status code, in this example, will be in

response.status

The error function will fire on anything that isn't a 200 --- so you will be best off using only the complete function, and simply putting your error in the default block of a switch-case statement...

[...]
complete: function(response){
    switch(response.status){
        case 201: 
           alert("success"
        break;
        default:
            alert("Something unexpected occurred"); 
        break;
}
rm-vanda
  • 3,122
  • 3
  • 23
  • 34
  • when i do this, my response object looks like this: { "readyState": 0, "status": 0, "statusText": "error" } this is the same regardless of the response header status code. – Tyler Gregory Oct 01 '14 at 15:22
  • So i added exactly as you suggested...despite seeing a 201 complete in the network console, it still alerts the default statement. response.status = 0 – Tyler Gregory Oct 01 '14 at 15:36
  • Then this is going to be your problem: http://stackoverflow.com/questions/2000609/jquery-ajax-status-code-0 – rm-vanda Oct 01 '14 at 16:48
0

Just use the complete handler - it will run after success or error have been called. Either way, for both complete and error, the first parameter is a jqXHR object, which has a status property which will give you the status of the response.

$.ajax({
    type: 'POST',
    url: url,
    dataType: 'text',
    complete: function(xhr) {
        //this handler runs regardless of success or error
        //query xhr.status for the response code
    }
});
James Thorpe
  • 31,411
  • 5
  • 72
  • 93
  • ive tried this. the problem is that the xhr object is of no help to me. { "readyState": 0, "status": 0, "statusText": "error" } – Tyler Gregory Oct 01 '14 at 15:30
0

If I well mean this, u want check a respons from server (201, 200, 500): maybe use .awlways()

$.ajax({
   type: 'POST',
   url: url,
   dataType: 'text',
   always: function(status){
      alert(status);
   }
});

or u can use a getAllResponseHeaders() on xhr object ?

Daredzik
  • 422
  • 2
  • 9
  • 21
0

You can use statusCode

$.ajax({
   type: 'POST',
   url: url,
   statusCode: {
      200: function (response) {
         alert('200');
      },
      201: function (response) {
         alert('201');
      },
      404: function (response) {
         alert('400');
      }
   }, success: function () {
      alert('Success');
   },
});
kums
  • 2,661
  • 2
  • 13
  • 16
  • Ive tried this, it never hits these statusCodes. These pull the statusCode form the body of the response, for which mine is undefined. – Tyler Gregory Oct 01 '14 at 15:18
  • 1
    I think you may be confusing terminology, the status code is in the header of the response (you showed the headers giving a 201), there is not status code in the body of a response. – Adam Jenkins Oct 01 '14 at 15:22
  • you may be right, but either way, it never alerts my. i can see the response in the console being 201 created but it never alerts 201 for me. – Tyler Gregory Oct 01 '14 at 15:28
  • I have removed `data:` from previous code. Try if the alerts are coming. – kums Oct 01 '14 at 15:35
  • @kums no luck there either. my problem is that the response.status = 0 for all of my responses. yet, when i watch the network console, i can see the status codes to be what they actually should be, 201 and 404. – Tyler Gregory Oct 01 '14 at 15:39