如何向localhost web api发出CORS请求 [英] How do I make CORS request to localhost web api
本文介绍了如何向localhost web api发出CORS请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我正在使用带角度js的MVC 4应用程序并调用另一个URL上托管的Web API。
两者都是我的本地主机但端口不同。
Web API:
[HttpPost]
< span class =code-keyword> public int SaveClient(TB_Client客户端)
{
ClientManager cmObj = new ClientManager();
return cmObj.AddModifyClient(client);
}
Web API配置文件:
< span class =code-keyword>< system.webServer >
< httpProtocol >
< customHeaders >
< add < span class =code-attribute> name = Access-Control-Allow-Origin value = * / >
< add name = Access-Control-Allow-Headers value = Content-Type / >
< add 名称 = 访问权限 - Control-Allow-Methods value = GET,POST,PUT,DELETE,OPTIONS / >
< / customHeaders >
< span class =code-keyword>< / httpProtocol >
< / system.webSerer >
调用此网址的MVC应用程序:
$ http.post(' http:// localhost:54212 / api / Client / SaveClient' ,$ scope.ClientObj).success( function (data,status,header,config){
alert(' 客户端添加');
})。
错误( function (){
alert( 错误);
});
但我仍然收到错误:
XMLHttpRequest无法加载http:// localhost:54212 / api / Client / SaveClient。对预检请求的响应未通过访问控制检查:请求的资源上不存在Access-Control-Allow-Origin标头。因此不允许来源'http:// localhost:52298'访问。响应的HTTP状态代码为405.
请指导我缺少的地方。
谢谢:)
解决方案
http.post(' http:// localhost:54212 / api / Client / SaveClient',
scope.ClientObj).success( function (data ,status,header,config){
alert(' 添加客户');
})。
错误( function (){
alert( 错误);
});
但我仍然收到错误:
XMLHttpRequest无法加载http:// localhost:54212 / api / Client / SaveClient。对预检请求的响应未通过访问控制检查:请求的资源上不存在Access-Control-Allow-Origin标头。因此不允许来源'http:// localhost:52298'访问。响应的HTTP状态代码为405.
请指导我缺少的地方。
谢谢:)
不确定你有什么样的设置,但也许这些文章可以帮到你。
我想向我的服务器添加CORS支持 [ ^ ]
HTML5 Rocks:使用CORS [ ^
Hi All,
I am using MVC 4 application with angular js and calling Web API hosted on another url.
Both are my localhost but port are different.
Web API:
[HttpPost]
public int SaveClient(TB_Client client)
{
ClientManager cmObj = new ClientManager();
return cmObj.AddModifyClient(client);
}
Web API config file:
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*"/>
<add name="Access-Control-Allow-Headers" value="Content-Type" />
<add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
</customHeaders>
</httpProtocol>
</system.webSerer>
MVC application that calling this web api:
$http.post('http://localhost:54212/api/Client/SaveClient', $scope.ClientObj).success(function (data, status, header, config) {
alert('Client added');
}).
error(function () {
alert("error");
});
But I still getting the error:
XMLHttpRequest cannot load http://localhost:54212/api/Client/SaveClient. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:52298' is therefore not allowed access. The response had HTTP status code 405.
Please guide me where I am lacking.
Thanks :)
解决方案
http.post('http://localhost:54212/api/Client/SaveClient',
scope.ClientObj).success(function (data, status, header, config) { alert('Client added'); }). error(function () { alert("error"); });
But I still getting the error:
XMLHttpRequest cannot load http://localhost:54212/api/Client/SaveClient. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:52298' is therefore not allowed access. The response had HTTP status code 405.
Please guide me where I am lacking.
Thanks :)
Not sure what kind of setup you have, but maybe these articles can help you.
I want to add CORS support to my server[^]
HTML5 Rocks: Using CORS[^]
这篇关于如何向localhost web api发出CORS请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文