ionic post 在启用 Cors 的服务中出现 rest 服务错误
ionic post in rest service error with Cors enabled service
您好,我是 ionic.I 的新手,想在 ionic 中调用 post 服务,但我总是遇到此错误;
""
Failed to load http://mywebservice.com/api: Response to preflight request doesn't pass access control check: The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'. Origin 'http://localhost:8100' is therefore not allowed access. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.
我在我的 Web 服务 (nancyfx) 中启用了 Cors,即使我在高级 Rest 服务上检查它。
并在 ionic 中调用 post 服务;
let headers = new HttpHeaders();
headers = headers
.set("Accept", "application/json")
.set("Content-Type", "application/json")
const params = new HttpParams();
const options = { headers, params, withCredentials: true };
return this.http
.post(
"mywebservice.com/api", headers, options
)do(res => console.log(res));
enter code here
任何帮助都是适用的,谢谢!
在您的 ionic .config() 中,您需要设置以下行
delete $httpProvider.defaults.headers.common['X-Requested-With'];
$httpProvider.defaults.useXDomain = true;
我终于通过在 web.config 中添加这些解决了我的问题(在我的网络服务中启用 cors,nancyfx),(注意将自定义 headers 添加到 Access-Control-Allow-Headers)
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="http://localhost:8100"/>
<add name="Access-Control-Allow-Credentials" value="true"/>
<add name="Access-Control-Allow-Headers" value="Origin, X-Requested-With, Content-Type, Accept, Authorization, Content-Disposition, mycustomheaders!" />
<add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS"/>
</customHeaders>
</httpProtocol>
您好,我是 ionic.I 的新手,想在 ionic 中调用 post 服务,但我总是遇到此错误; ""
Failed to load http://mywebservice.com/api: Response to preflight request doesn't pass access control check: The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'. Origin 'http://localhost:8100' is therefore not allowed access. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.
我在我的 Web 服务 (nancyfx) 中启用了 Cors,即使我在高级 Rest 服务上检查它。
并在 ionic 中调用 post 服务;
let headers = new HttpHeaders();
headers = headers
.set("Accept", "application/json")
.set("Content-Type", "application/json")
const params = new HttpParams();
const options = { headers, params, withCredentials: true };
return this.http
.post(
"mywebservice.com/api", headers, options
)do(res => console.log(res));
enter code here
任何帮助都是适用的,谢谢!
在您的 ionic .config() 中,您需要设置以下行
delete $httpProvider.defaults.headers.common['X-Requested-With'];
$httpProvider.defaults.useXDomain = true;
我终于通过在 web.config 中添加这些解决了我的问题(在我的网络服务中启用 cors,nancyfx),(注意将自定义 headers 添加到 Access-Control-Allow-Headers)
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="http://localhost:8100"/>
<add name="Access-Control-Allow-Credentials" value="true"/>
<add name="Access-Control-Allow-Headers" value="Origin, X-Requested-With, Content-Type, Accept, Authorization, Content-Disposition, mycustomheaders!" />
<add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS"/>
</customHeaders>
</httpProtocol>