最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

javascript

    $('#send').on('click', function() {
        $.ajax({
            'url': $('#url').val(),
            'type': 'post',
            'plete': 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

javascript

    $('#send').on('click', function() {
        $.ajax({
            'url': $('#url').val(),
            'type': 'post',
            'plete': 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
Share Improve this question edited Dec 7, 2011 at 23:09 Justin808 asked Dec 7, 2011 at 21:36 Justin808Justin808 21.5k48 gold badges165 silver badges273 bronze badges 8
  • 2 I'm not sure that's standard pliant for HTTP... – Alex Turpin Commented Dec 7, 2011 at 21:38
  • Yup, it is oddly enough. w3/Protocols/rfc2616/rfc2616-sec6.html#sec6.1 Only the code is required to be a specific code. (Edit: At least in HTTP 1.1. Finding 1.0 now..) – Corbin Commented Dec 7, 2011 at 21:41
  • Just send your own custom header with that message. – aziz punjani Commented Dec 7, 2011 at 21:42
  • @Xeon06 it is, I've added a link. – Justin808 Commented Dec 7, 2011 at 21:43
  • Well, it's valid if it's 200 (success) not 200(success). – Corbin Commented Dec 7, 2011 at 21:44
 |  Show 3 more ments

2 Answers 2

Reset to default 4

Got it. It's jqXHR.statusText.

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

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

Have you tried xhrobject.getResponseHeader() yet? jQuery docs say it's also available there. If you don't know the header's name, try getAllResponseHeaders().

Also, can you see that message in your browser's debugging console (network tab, connection headers)? If it's not there, it will hardly be available from js.

发布评论

评论列表(0)

  1. 暂无评论