我正在尝试执行 $http.post,但此响应给了我此错误。
XMLHttpRequest cannot load http://XXXXXXXXX:2222/server/api/v1/controllers. Response for preflight has invalid HTTP status code 403
我在互联网上读到的所有内容都说这是 CORS 的错误,我尝试解决此问题但没有成功。
这是我的 $http.post 方法。
// Set the Content-Type
$http.defaults.headers.post["Content-Type"] = "application/json";
// Delete the Requested With Header
delete $http.defaults.headers.common['X-Requested-With'];
var config = {
method: 'POST',
headers: {'Content-Type': 'application/json; charset=utf-8'}
}
$http.post("http://XXXXXXXXX:2222/server/api/v1/controllers", objToSave , config)
.then(function successCallback(response) {
// this callback will be called asynchronously
// when the response is available
console.log(response)
}, function errorCallback(error) {
// called asynchronously if an error occurs
// or server returns response with an error status.
console.log(error)
});
我使用的是 Angular v1.5.5。
希望你能帮助我,
问候!
最佳答案
chrome 首先发送预检请求来检查服务器是否正确响应。如果服务器正确处理该预检请求,则原始请求将由 chrome 发送。尝试在服务器请求处理程序 header 中添加以下行。
Access-Control-Allow-Origin: *
关于javascript - CORS 错误 - Angularjs $http.post,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42225763/