启用了Cors的服务后,离子在休息服务中出现错误 [英] ionic post in rest service error with Cors enabled service

查看:105
本文介绍了启用了Cors的服务后,离子在休息服务中出现错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我是ionic的新手.我想打电话给ionic服务,但是我总是会收到此错误; "

Hello I'm a newbie in ionic.I want to call post service in ionic but I always get this error ; ""

无法加载 http://mywebservice.com/api :对预检请求的响应未通过访问控制检查:当请求的凭据模式为包括"时,响应中访问控制允许-原始"标头的值不得为通配符"*".因此,不允许访问来源' http://localhost:8100 . XMLHttpRequest发起的请求的凭据模式由withCredentials属性控制.

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.

即使在高级Rest Service上也已在我的Web服务(nancyfx)中启用了Cors.

I have enable Cors in my web service (nancyfx) Even I check it on advanced Rest Service .

并致电ionic的邮政服务部门;

And call post service in ionic ;

     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

谢谢您的帮助!

推荐答案

我终于通过在web.config中添加这些内容来解决我的问题(在我的webservice中启用cors,nancyfx),(注意将自定义标头添加到Access -Control-Allow-Headers)

I finally solve my problem by adding these in web.config (enable cors in my webservice ,nancyfx) ,(be aware of adding your custom headers to the 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>

这篇关于启用了Cors的服务后,离子在休息服务中出现错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆