400 Bad Request
将我的 Backbone.js/node.js 应用程序部署到 Heroku 时出现错误。
从日志中:
Error: Bad Request
at Object.error (/app/node_modules/express/node_modules/connect/lib/utils.js:42:13)
at IncomingMessage.<anonymous> (/app/node_modules/express/node_modules/connect/lib/middleware/json.js:57:73)
at IncomingMessage.emit (events.js:64:17)
at HTTPParser.parserOnMessageComplete [as onMessageComplete] (http.js:130:23)
at Socket.ondata (http.js:1506:22)
at TCP.onread (net.js:374:27)
从 Chrome devtools 的网络选项卡:
运行时不会出现该错误localhost
。
有趣的是,直接访问 URL 可以正常工作。手动jQuery.ajax()
调用也可以正常工作。此Backbone.sync
调用是资源失败的唯一实例,但它始终失败。
部署到 Heroku 时,这个 Backbone/node/express 400 Bad Request 错误是怎么回事?我的猜测可能与代理/路由器工件有关,例如x-forwarded-for
但此时我无法判断错误是在 Backbone、express 还是 Heroku 级别。
骨干网请求标头(400 错误请求)
GET /services HTTP/1.1
Host: karmr.herokuapp.com
Connection: keep-alive
Cache-Control: max-age=0
X-Requested-With: XMLHttpRequest
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_4) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1082.0 Safari/536.5
Content-Type: application/json
Accept: application/json, text/javascript, */*; q=0.01
Referer: http://karmr.herokuapp.com/
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
HTTP/1.1 400 Bad Request
Content-Type: text/plain
X-Powered-By: Express
Content-Length: 420
Connection: keep-alive
直接GET
(200 OK):
GET /services HTTP/1.1
Host: karmr.herokuapp.com
Connection: keep-alive
Cache-Control: max-age=0
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_4) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1082.0 Safari/536.5
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
HTTP/1.1 200 OK
Content-Type: text/html; charset=utf-8
X-Powered-By: Express
Content-Length: 84
Connection: keep-alive
以下是标头差异(此处为较大版本):