jquery how to get the status message returned by a

2020-08-19 02:33发布

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

2条回答
Deceive 欺骗
2楼-- · 2020-08-19 02:41

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.

查看更多
3楼-- · 2020-08-19 03:00

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.

查看更多
登录 后发表回答