INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Response data from $.ajax request failure

Response data from $.ajax request failure

(OP)
Hi,

I have an ajax request, I return the following...

CODE

print "Content-type: text/html\n";
print "Status: 400 Bad Request\n\n";
print $template->output; 

But I cannot find the returned data anywhere in the ajax response

CODE

error: function(jqXHR){
            showDialog(jqXHR.responseText,'AJAX Failed');
        } 

All that does is show me 'Bad Request'?

How do I get access to the actual returned HTML output from the 400 response?

Success works fine....

CODE

success: function(data){
            showDialog(data,'AJAX Success');
        } 

So where is the data stored in an 'error' return?

Thanks,
1DMF.

"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"
Free Electronic Dance Music

RE: Response data from $.ajax request failure


Quote:

All that does is show me 'Bad Request'?

Why are you surprised at that when that is exactly what you send as the response?

CODE --> php

print "Status: 400 Bad Request\n\n"; 

User agents don't hang around waiting for more data after a HTTP: 4xx response is given.


Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum

RE: Response data from $.ajax request failure

(OP)
I've given up as I don't seem to be getting any returned data if error code not a 2xx , so I've refactored my success to deal with 207 status code

CODE

success: function(data,status,jqXHR){  
        if(jqXHR.status === 207)
        {                         
            showDialog(data);
        }
        if(jqXHR.status === 200)
        {                         
            uploadComplete();
        }                                              
    } 

It seem even when server side has failed , you have to return a 2xx status code if you want the Ajax to receive any result data (in this case bespoke error messages)

"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"
Free Electronic Dance Music

RE: Response data from $.ajax request failure

Send the appropriate error code and your status text.

User agents don't care what it says, they just want the correct number.

You could send

CODE --> php

print "Status: 400 Don't be so damned stupid\n\n"; 

or

CODE --> php

print "Status: 400 Hasta La Vista Baby!\n\n"; 

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum

RE: Response data from $.ajax request failure

Send it as a header instead of a print.

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum

RE: Response data from $.ajax request failure

(OP)
Yes, I was... I think because I was wrapping a HTML template around the message which included a html header etc.. it was too malformed for a string message in the header output.

However, I have refactored the error handling...

CODE

print "Content-type: text/html\n";
print "Status: 400 $error_txt[$_[0]]\n\n"; 

And revamped the AJAX code...

CODE

error: function(jqXHR,textStatus,errorThrown){
            showDialog(errorThrown,'AJAX Failed');
        } 

It is now working as desired, so good to know I was on the right track.

Many thanks Chris, appreciated.

"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"
Free Electronic Dance Music

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close