开发者

Why is a linefeed char added to the http response?

I do this ajax request:

 var response = $.ajax({
    url: 'product/add', 
    data: $("#formAddNewRow").serialize(),
    type: "POST",
    success: function() {
    var id = response.responseText;     
                        }            
                      });  

'product/add' is a symfony action that does some stuff. the action's view is what gets returned, for testing purpose that file looks like this:

<?php

 echo "test";

?>

When I look at response.responseText right after success, I get "test\n". I would have expected just "test".

This is what a Response Header looks like:

Date    Thu, 22 Sep 2011 15:17:45 GMT
Server  Apache/2.2.17 (Win32) mod_ssl/2.2.17 OpenSSL/0.9.8o PHP/5.3.4 mod_perl/2.0.4 Perl/v5.10.1
X-Powered-By    PHP/5.3.5
Expires Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control   no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma  no-cache
Content-Length  5
Keep-Alive  timeout=5, max=99
Connection  Keep-Alive
Content-Type    text/html; charset=utf-8

Somewhere else I do an ajax request to url: 'product/update', it's view being exactly the same code (<?php echo "test"; ?>). But in this case response.responseTextequals "test", which is what I expect - there is no \n added. In that case, a Response Header looks like:

Date    Thu, 22 Sep 2011 15:27:20 GMT
Server  Apache/2.2.17 (Win32) mod_ssl/2.2.17 OpenSSL/0.9.8o PHP/5.3.4 mod_perl/2.0.4 Perl/v5.10.1
X-Powered-By    PHP/5.3.5
Expires Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control   no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma  no-cache
Content-Length  4
Keep-Alive  timeout=5, max=99
Connection  Keep-Alive
Content-Type    text/html; charset=utf-8 

I have not the slightest idea, why response.responseText whouldn't be the same in both开发者_运维知识库 cases.

Why does a newline character get added to response.responseText?


I would guess that there is a linefeed character after the final ?>

Unless I am actually putting PHP inside a HTML page, I always leave off the last ?>. It isn't necessary, and tends to cause problems like this.


Because it's part of the HTTP protocol?

   Response      = Status-Line               ; Section 6.1
                   *(( general-header        ; Section 4.5
                    | response-header        ; Section 6.2
                    | entity-header ) CRLF)  ; Section 7.1
                   CRLF
                   [ message-body ]          ; Section 7.2
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜