有人见过这个吗?
如果我没有在我的角度控制器$ http post中包含数据参数,那么快速侧路线将被选中并运行,但是如果我添加了一个数据元素,它会挂起并且不会选择路线 - 最终Chrome调试器显示错误:net :: ERR_EMPTY_RESPONSE。
这有效:
$scope.newAccount = function(){
console.log("Got a submit request for new account: " );
$http.post("http://localhost:3000/newAccount").success(function( data, status, headers, config ){
console.log("SUCCESS on NEW ACCOUNT");
$scope.mode = data;
}).error(function(data, status, headers, config) {
console.log("Got ERROR");
});};
这不起作用:
$scope.newAccount = function(){
console.log("Got a submit request for new account: " );
$http.post("http://localhost:3000/newAccount",{info:"toSend"}).success(function( data, status, headers, config ){
console.log("SUCCESS on NEW ACCOUNT");
$scope.mode = data;
}).error(function(data, status, headers, config) {
console.log("Got ERROR");
});
};
这是Express处理程序,它不会更改(实际逻辑被删除):
app.post('/newAccount', function (req, res) {
console.log("GOT A POST REQUEST: " );
....some code here
res.setHeader("Access-Control-Allow-Origin", "*");
res.send( "successful post" );
});
答案 0 :(得分:0)
因此,事实证明这个问题与远程访问授权有关。 Chrome调试器提供了一些相当蹩脚的信息。 Firefox调试器更加清晰。以前我设置了res.header(“Access-Control-Allow-Origin”,“*”);在发送响应之前直接在路由处理程序中。这为没有数据元素的原始调用修复了同样的问题。但是,为了使其与数据元素一起使用,必须将以下内容添加到我的Express应用程序中 - 而不是在路由处理程序本身中进行设置。确实是一个更好的编程实践,但我只是在讨论一些东西以了解Node / Express。在这一个上浪费了很多时间。
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});