我想在我的网络服务中发布输入值。我检查的时候错误很多,发现用的是OPTIONS而不是POST
这是我的代码:
var req = {
headers: {
'Content-Type':'application/json'
}
}
$http.post(url,req)
.then(
function(response){
},
function(response){
});
很奇怪为什么我有 OPTIONS 而不是 POST,我该如何解决这个问题? (我正在使用 AngularJS)
最佳答案
从 access-control-request-method
header 中,您正在查看 CORS 预检请求。当您跨源时,在发送实际请求之前,会发生这些情况。服务器可能会根据源来源、方法等,使用 header 进行响应,以指示是否允许实际请求。
来自the MDN docs ,任何发送JSON的POST请求都会有相应的预检请求:
In particular, a request is preflighted if:
- It uses methods other than GET, HEAD or POST. Also, if POST is used to send request data with a Content-Type other than application/x-www-form-urlencoded, multipart/form-data, or text/plain, e.g. if the POST request sends an XML payload to the server using application/xml or text/xml, then the request is preflighted.
关于javascript - 使用 OPTIONS 方法代替 POST,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37684074/