我有一个基于Node.js和Express的应用程序,它作为Emberjs RESTAdapter的REST api。现在我正在尝试将其部署到Windows Azure上。当Ember向"/examples/1"发出HTTP请求时,会出现400 Bad Request错误。
经过故障排除后,似乎该错误取决于请求头内容类型。
- Visiting the "(domain)/users/1" in a web-browser prints out the correct json in the browser window.
Running Jquery ajax calls gives me the following results:
This does not work:
$.ajax({ url: "/users/1", contentType: "application/json; charset=UTF-8" }).done(function(data) { console.log(data); });
But, when switching to this, it works.
$.ajax({ url: "/users/1", contentType: "application/x-www-form-urlencoded; charset=UTF-8" }).done(function(data) { console.log(data); });
更新
以下是服务器端代码。
var express = require('express');
var app = express();
app.configure(function(){
app.set('port', process.env.PORT || 3000);
app.use(express.bodyParser());
app.use('/', express.static(__dirname + '/WebContent'));
app.use(express.cookieParser());
});
app.get('/:param/:id', function(req, res) {
res.json(200, {user: {id:1, name: 'tester'}} )
});
app.get('/:param', function(req, res) {
res.json(200, {users: [{id:1, name: 'tester'}]})
});
app.listen(app.get('port'), function(){
console.log("Express server listening on port " + app.get('port'));
});