【发布时间】:2015-09-15 13:24:31
【问题描述】:
所以,当我尝试发送 POST 请求时,它没有成功。 Chrome 提示我这个错误:
OPTIONS http://localhost:49475/api/Kpi/new (anonymous function) @ angular.js:10661sendReq @ angular.js:10480serverRequest @ angular.js:10187processQueue @ angular.js:14634(anonymous function) @ angular.js:14650Scope.$eval @ angular.js:15878Scope.$digest @ angular.js:15689Scope.$apply @ angular.js:15986(anonymous function) @ angular.js:23376n.event.dispatch @ jquery-2.1.4.min.js:3r.handle @ jquery-2.1.4.min.js:3
Index.html:1 XMLHttpRequest cannot load http://localhost:49475/api/MYLINK. Response for preflight has invalid HTTP status code 405
我非常怀疑我的请求是否存在问题,但它看起来像这样:
return $http.post(apiAddress + 'mylink', dataObj)
.then(function (response) {
console.log("succeeded");
return response.status;
}, function (response) {
console.log("failed");
return response.status;
});
我已在我的 API 部分(服务器一)上安装了 CORS,并在 WebApiConfig 中启用了它:
var cors = new EnableCorsAttribute("*", "*", "*");
config.EnableCors(cors);
我还编辑了我的 Web.config 文件并启用了所有访问控制变量:
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Methods" value="GET, PUT, POST, DELETE, OPTIONS" />
<add name="Access-Control-Allow-Headers" value="Origin, X-Requested-With, Content-Type, Accept" />
</customHeaders>
</httpProtocol>
我的客户端在 localhost:57810 上运行,而服务器端在 localhost:49475 上。我一直在寻找这个工作近 6 个小时。我已经尝试了所有可以在网上找到的可能的 sn-p 和“解决方案”,但都没有成功。如果是这样,我就不会发布这个问题了......
感谢我得到的任何答案...
编辑:Post 方法按预期工作,我已经用 Postman 对其进行了测试。现在,向上述 url (api/mylink) 发送 OPTIONS 请求不起作用。
返回:
{"Message":"The requested resource does not support http method 'OPTIONS'."}
【问题讨论】:
-
您是否尝试将
[EnableCors(origins: "*", headers: "*", methods: "*")]放在您的 ApiController 上? -
我刚试过,很遗憾它不起作用。
-
请贴上action方法的签名
标签: asp.net angularjs rest http http-headers