2016-03-15 59 views
6

我升级我的科尔多瓦iOS应用程序到最新版本:科尔多瓦iOS的错误:地空没有被允许访问控制允许来源

iOS 4.1.0 
Cordova 6.0.0 

而且我已经更新了插件和还增加新的WKWebView引擎插件。

我已经将我的内容安全,策略index.html文件:

<meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self' 'unsafe-inline' 'unsafe-eval'; style-src 'self' 'unsafe-inline'; img-src 'self' 'unsafe-inline' data:;connect-src 'self' https://mydomain/myservice/; plugin-types application/pdf"> 

我config.xml中总是包含:

<content src="index.html" /> 
<access origin="*" /> 

,但我加

<access origin="*.mydomain.*" /> 

为好措施。

我控制的应用程序试图连接到Web服务器,并已启用了CORS它:

<httpProtocol> 
    <customHeaders> 
     <add name="Access-Control-Allow-Origin" value="*" /> 
    </customHeaders> 
    </httpProtocol> 

该服务器有一个有效的SSL证书和所有它的调用是https://开头。

我在Xcode的plist有:

Allow Arbitrary Loads : Yes 
Exception Domains 
    [mydomain] 
     NSIncludesSubdomains : Yes 
     NSTemporaryExceptionAllowsInsecureHTTPLoads : YES 
     NSTemporaryExceptionMinimumTLSVersion : TLSv1.1 

我建立并直接上运行的应用程序的设备和使用Safari开发,以查看错误。

我有这个应用程序的Android版本,它有自己更新的基本代码,它工作正常。

我的进一步研究表明,它是导致问题的新的WKWebView引擎插件。但是我发现的唯一建议是在Ionic论坛中,而且我没有在这个版本中使用Ionic。

只是为了确认,这些都是我从Safari浏览器获得在调试错误消息:

[Error] Failed to load resource: the server responded with a status of 405 (Method Not Allowed) ([my thing], line 0) 
[Error] Failed to load resource: Origin null is not allowed by Access-Control-Allow-Origin. ([my thing], line 0) 
[Error] XMLHttpRequest cannot load https://[mydomain]/[my service]/[my thing]. Origin null is not allowed by Access-Control-Allow-Origin. 
+0

看起来CORS在服务器上没有正确设置。你的请求是否有auth头文件? – jcesarmobile

+0

不,我错过了一些东西。在下面的答案中添加了它。 – DaveSav

回答

1

@jcesarmobile是正确的。简单地将httpProtocol添加到我的web.config是不够的。我还必须将以下代码添加到global.asax Application_BeginRequest

HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*") 
    If HttpContext.Current.Request.HttpMethod.Equals("OPTIONS") Then 
     HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "POST, PUT, DELETE") 
     HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept") 
     HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000") 
     HttpContext.Current.Response.End() 
    End If 
相关问题