有問題將數據從http post請求回送到我正在構建的API。拋出錯誤:CORS錯誤:Angular.JS,Node.JS和Express
XMLHttpRequest cannot load (URL to API here). No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin ' http://localhost:9000 ' is therefore not allowed access.
下面是在客戶端的角度代碼:
$http.post('MyAPI'sURLHere', {date: $scope.information.PubDate})
.then(function(response){
console.log(response);
}, function(error){
console.log(error);
});
下面是我的API節點服務器端代碼:
app.post('/getThing', function(req, res){
var date = req.body.date;
console.log(typeof date);
var query = Overquery
var query2 = "alter session set nls_date_format = 'MM/dd/yyyy'";
console.log(query);
oracleDB.execute(query2, function(err, result){
if(err){
console.log(err.message);
}
else{
console.log(result);
}
});
oracleDB.execute(query, function(err, result){
if(err){
console.log(err.message);
}
else{
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'POST');
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization');
console.log(result.rows);
res.json(result.rows);
}
});
});
第一次建立一個API所以任何建議和幫助將不勝感激!
你正確設置CORS標頭的POST請求,然而,你可能在某些情況下,沒有正確迴應發出請求之前的OPTIONS請求。 –