Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery how to get the status message returned by a ajax call of type post?

javascript

    $('#send').on('click', function() {
        $.ajax({
            'url': $('#url').val(),
            'type': 'post',
            'complete': function (jqXHR, textStatus) {
                var msg = "Status: " + jqXHR.status + " (" + jqXHR.statusText + " - " + textStatus + ")<br />";
                msg += jqXHR.getAllResponseHeaders().replace(/\n/g, "<br />");

                $('#results').html(msg);
            }
        });
    });

php

    header("HTTP/1.0 200 Some message here");
    flush();
    exit();

Results

Status: 200 (OK - success)
Date: Wed, 07 Dec 2011 21:57:50 GMT 
X-Powered-By: PHP/5.3.6 
Transfer-Encoding: chunked 
Connection: Keep-Alive 
Server: Apache/2.2.17 (Unix) mod_ssl/2.2.17 OpenSSL/0.9.8r DAV/2 PHP/5.3.6 
Content-Type: text/html 
Keep-Alive: timeout=5, max=100 

Question

How do I get the "Some message here" part of the header?

http

http protocol

6.1 Status-Line

The first line of a Response message is the Status-Line, consisting of the protocol version followed by a numeric status code and its associated textual phrase, with each element separated by SP characters. No CR or LF is allowed except in the final CRLF sequence.

   Status-Line = HTTP-Version SP Status-Code SP Reason-Phrase CRLF
like image 971
Justin808 Avatar asked Dec 07 '11 21:12

Justin808


People also ask

How do I get the HTTP status code with jQuery?

jQuery Ajax Handling HTTP Response Codes with $. always promise callbacks, which are triggered based on whether the request was successful or not, there is the option to trigger a function when a specific HTTP Status Code is returned from the server. This can be done using the statusCode parameter. $.

What does an AJAX call return?

ajax() function returns the XMLHttpRequest object that it creates. Normally jQuery handles the creation of this object internally, but a custom function for manufacturing one can be specified using the xhr option.


1 Answers

Got it. It's jqXHR.statusText.

$.get("test.php").complete(function(jqXHR) {
    console.log(jqXHR.statusText);
});

Just tried it out in Chrome with your exact PHP code.

like image 183
Alex Turpin Avatar answered Oct 07 '22 16:10

Alex Turpin