这是我的Angular js片段代码:
$http({
method:'POST',
withCredential:true,
url:$scope.config.app_ws+'auth/signup',
data:{user:$scope.auth}
}).success(function(status, response){
console.log(response);
}).error(function(status, response){
alert(response+'Bummer :( , an error occured plese retry later. ');
});
这是我的Node.js片段后端:
var allow_cross_domain= function(req, res, next) {
res.header('X-Powered-By', 'hey.heyssssssss.org');
var oneof = false;
if(req.headers.origin) {
res.header('Access-Control-Allow-Origin', req.headers.origin);
oneof = true;
}
if(req.headers['access-control-request-method']) {
res.header('Access-Control-Allow-Methods', req.headers['access-control-request-method']);
oneof = true;
}
if(req.headers['access-control-request-headers']) {
res.header('Access-Control-Allow-Headers', req.headers['access-control-request-headers']);
oneof = true;
}
if(oneof) {
res.header('Access-Control-Max-Age', 60 * 60 * 24 * 365);
}
// intercept OPTIONS method
if (oneof && req.method == 'OPTIONS') {
res.send(200);
} else {
next();
}
}
app.use(allow_cross_domain);
app.post('/auth/signup', function (req, res) { res.send('wtff'); });
我只是从Angular调用POST localhost:3000 / auth / signup到Node,但是我进入**CAUTION : Provisional headers are shown.**
了chrome控制台。
镀铬(注意):
Firefox(大约30/60秒没有响应,然后出现alert():/):
这可能是什么?
如果我使用GET的话,一切都很好,只是使用POST麻烦了,那怎么可能呢?
个人而言,我在角度使用此“重置”方法:
app.config(['$httpProvider', function ($httpProvider) {
//Reset headers to avoid OPTIONS request (aka preflight)
$httpProvider.defaults.headers.common = {};
$httpProvider.defaults.headers.post = {};
$httpProvider.defaults.headers.put = {};
$httpProvider.defaults.headers.patch = {};
}]);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句