我的Web API 2解决方案中有一个oData控制器,并且试图从我的angularJs应用程序调用Delete动作。我试过了:
$http({
method: 'DELETE',
url: "http://localhost:52389/odata/Bears('" + $scope.itemKey + "')",
headers: { 'Content-Type': 'application/json' }
}).then(function successCallback(response) {
console.log('success', response);
}, function errorCallback(response) {
console.log('error', response);
});
在提琴手看来,这似乎向API发送了一个OPTIONS请求。在提琴手中检查发送的请求,我可以看到这就是发送的内容:
OPTIONS http://localhost:52389/odata/Bears('1') HTTP/1.1
Host: localhost:52389
Connection: keep-alive
Access-Control-Request-Method: DELETE
Origin: http://localhost
X-FirePHP-Version: 0.0.6
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.71 Safari/537.36
Access-Control-Request-Headers: accept
Accept: */*
Referer: http://localhost/?id=places
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-GB,en-US;q=0.8,en;q=0.6
如果我在小提琴手中更改第一行,则:
OPTIONS http://localhost:52389/odata/Bears('1') HTTP/1.1
到:
DELETE http://localhost:52389/odata/Bears('1') HTTP/1.1
然后这个工作。如何从angularJs中获取“删除”方法?
发送OPTIONS的原因是您要发送跨域请求。服务器需要首先同意这一点,因此需要第一个OPTIONS请求。
如果在OPTIONS之后未发送DELETE,则服务器的响应很可能为否,这意味着您需要在服务器上配置CORS。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句