Firefox正在解析空响应有效负载

Seb*_*äni 13 javascript firefox http

我们正在使用以下标题发出XHR请求(我简化了一下):

POST http://localhost:9001/login

Host: localhost:9001
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:51.0) Gecko/20100101 Firefox/51.0
Accept: application/json, text/plain, */*
Content-Type: application/json;charset=utf-8
Content-Length: 67
Run Code Online (Sandbox Code Playgroud)

然后我们的服务器响应这样(再次简化):

Status code: 200 OK

Cache-Control: no-cache, no-store
Connection: close
Content-Length: 0
Date: Mon, 27 Feb 2017 17:19:53 GMT
Server: WildFly/9
Set-Cookie: JSESSIONID=123; path=/
Run Code Online (Sandbox Code Playgroud)

响应中没有有效负载.请注意Content-Length: 0.但Firefox仍然试图将其解析为XML.并将以下错误输出到控制台:

XML Parsing Error: no root element found 
Location: http://localhost:9001/login 
Line Number 1, Column 1
Run Code Online (Sandbox Code Playgroud)

请注意,服务器不发送content-type标头.根据RFC 7231,它只需要content-type在有实际内容时发送标头.

这是Firefox中的一个错误还是我的研究错误?

自己复制它

我写了一个小型服务器和客户端来重现问题.

server.js(开头node ./server.js):

const fs = require('fs'), http = require('http');
const server = http.createServer(function (request, response) {
    if (request.method === 'POST') {
        // send empty response
        response.end();
        return;
    }

    // send file content
    fs.readFile('.' + request.url, function (error, content) {
        response.writeHead(200, { 'Content-Type': request.url === '/index.html' ? 'text/html' : 'text/javascript' });
        response.end(content);
    });
}).listen(8080);
Run Code Online (Sandbox Code Playgroud)

index.html

<script src="client.js"></script>
Run Code Online (Sandbox Code Playgroud)

client.js

var xhr = new XMLHttpRequest();
xhr.open('POST', 'http://localhost:8080/login');
xhr.send();
Run Code Online (Sandbox Code Playgroud)

在Firefox中打开URL http:// localhost:8080/index.html时,JavaScript控制台中会出现错误.

Firefox版本51.0.1

Jon*_*nna 4

响应中没有有效负载。注意内容长度:0

这意味着存在有效负载,并且该有效负载的大小为 0 字节。null将和之间的差异视为""字符串。""当您想要 的等价物时,您在这里所拥有的就是等价物null

将状态代码设置为204而不是200指示您没有发送实体(并删除内容类型,因为不存在没有实体的内容类型)。

(很长一段时间,Firefox 仍然会在这种情况下记录错误,但幸运的是,这最终得到了修复。即使它确实记录了错误,它仍然会继续正确运行任何脚本)。