Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Response data from $.ajax request failure 1

Status
Not open for further replies.

1DMF

Programmer
Jan 18, 2005
8,795
GB
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
 

All that does is show me 'Bad Request'?

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

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
 
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
 
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
PHP:
print "Status: 400 Don't be so damned stupid\n\n";

or

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
 
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
 
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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top