PhoneGap支持Cors吗?

4
我是一名新手,希望将AngularJs应用程序嵌入phonegap中。在服务器端,我使用Apache Tomcat 7 CORS过滤器和Rest。当我在浏览器中运行该应用程序时,它可以正常工作。但是,当我在iOS或Android上使用phonegap运行该应用程序时,GET请求可以正常工作,但POST请求会返回403禁止的响应。我可以看到"Origin"头部值为"file://"。我认为问题在这里。
我的Tomcat CORS过滤器配置如下:
<filter>
    <filter-name>CorsFilter</filter-name>
    <filter-class>org.apache.catalina.filters.CorsFilter</filter-class>
    <init-param>
        <param-name>cors.allowed.origins</param-name>
        <param-value>*</param-value>
    </init-param>
    <init-param>
        <param-name>cors.allowed.methods</param-name>
        <param-value>GET,POST,HEAD,OPTIONS,PUT</param-value>
    </init-param>
    <init-param>
        <param-name>cors.allowed.headers</param-name>
        <param-value>customer,user,Delay,Content-Type,X-Requested-With,accept,Origin,Access-Control-Request-Method,Access-Control-Request-Headers</param-value>
    </init-param>
    <init-param>
        <param-name>cors.exposed.headers</param-name>
        <param-value>Access-Control-Allow-Origin,Access-Control-Allow-Credentials</param-value>
    </init-param>
    <init-param>
        <param-name>cors.support.credentials</param-name>
        <param-value>true</param-value>
    </init-param>
    <init-param>
        <param-name>cors.preflight.maxage</param-name>
        <param-value>10</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>CorsFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

当我从客户端进行POST请求时,我会得到以下结果:

Request URL:http://134.0.12.789:8081/test/rest-api/testing/add
Request Method:PUT
Status Code:403 Forbidden
Request Headersview source
Accept:application/json, text/plain, */*
Content-Type:application/json;charset=UTF-8
customer:Xyz
Origin:file:// 
user:user1
User-Agent:Mozilla/5.0 (Linux; Android 4.4.2; Nexus 7 Build/KOT49H) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/30.0.0.0 Safari/537.36
Request Payloadview source
{quantity:0.5, unit:KG, product:5791}
quantity: 0.5
unit: "KG"
product: 5791
Response Headersview source
Content-Length:0
Content-Type:text/plain
Date:Wed, 23 Jul 2014 10:43:14 GMT
Server:Apache-Coyote/1.1

我的www/config.xml和platform/android/res/xml/config.xml文件都添加了<access origin="*" />代码。

在app.js中添加以下代码启用CORS:

$httpProvider.defaults.useXDomain = true;
delete $httpProvider.defaults.headers.common['X-Requested-With'];

如果有人之前遇到过这个问题,请帮我解决一下。

https://dev59.com/42Ei5IYBdhLWcg3wZ7u8#21299355 - Raghavendra
我有完全相同的问题。我检查了/config.xml和/platforms/android/res/xml/config.xml,这两个文件都有<access origin="*" />。但仍然不起作用。我的服务器访问日志报告如下: <client-ip> - - [25/Jul/2014:22:27:15 +0000] "PUT /service/route HTTP/1.1" 403 5 <client-ip> - - [25/Jul/2014:22:34:45 +0000] "GET /service/some-route HTTP/1.1" 200 13 <client-ip> - - [25/Jul/2014:22:35:02 +0000] "POST /service/some-other-route HTTP/1.1" 403 5 <client-ip> - - [25/Jul/2014:22:36:24 +0000] "GET /service/another-route HTTP/1.1" 200 889 - ezabaw
那么你是如何克服这个问题的?我是否漏掉了什么东西?我花了很多时间让它工作,但似乎没有任何效果。 - Bil
由于您的服务器响应403 Forbidden,看起来这是一个服务器配置问题,请确保URL可访问(如果您使用apache+tomcat,则可以检查apache conf文件)。当您遇到CORS问题时,服务器会正常响应,但浏览器会阻止响应。 - QuickFix
不,我没有使用apache+tomcat...我只是使用tomcat。 - Bil
4个回答

2
是的,只需在您的 config.xml 文件中添加此行即可:
<access origin="http://example.com" />

你还可以这样做:
<access origin="*" />

但最好指定你发送请求的域名。

如果需要更多信息,请查看PhoneGap文档中的此页面


我已经在PhoneGap的platforms/android/config.xml和www/config.xml中添加了"<access origin="*" />"行,但是即使这样也没有改变。 - Bil
如果是<访问来源>的问题,那么即使使用GET方法也不应该起作用,因为它不接受该来源,我会收到404错误。但更糟糕的是,当GET方法起作用时,其他方法却无法正常工作。我已经尝试了几乎所有可能的方法,但找不到任何有用的东西。 - Bil
问题不应该来自于Phonegap客户端,而是来自于您的Tomcat服务器配置。 - Kaz

1
Taher的解决方案解决了我在PhoneGap+jQuery访问Tomcat web-service时的问题。只需将参考的Java代码(https://github.com/sebastienblanc/cors-filter/blob/master/src/main/java/org/ebaysf/web/cors/CORSFilter.java#L825)包含在我的项目中,并将下一行添加到项目的web.xml中。重新部署即可,无需在Tomcat的web.xml中添加corsFilter。现在它可以同时用于PhoneGap请求和浏览器请求。这明显是某个TomCat(或PhoneGap?)的bug。
<filter>
    <filter-name>CorsFilter</filter-name>
    <filter-class>myDeploymentName.CORSFilter</filter-class>
    <init-param>
        <param-name>cors.allowed.origins</param-name>
        <param-value>*</param-value>
    </init-param>
    <init-param>
        <param-name>cors.allowed.methods</param-name>
        <param-value>GET,POST,HEAD,OPTIONS,PUT</param-value>
    </init-param>
    <init-param>
        <param-name>cors.allowed.headers</param-name>
        <param-value>Content-Type,X-Requested-With,accept,Origin,Access-Control-Request-Method,Access-Control-Request-Headers</param-value>
    </init-param>
    <init-param>
        <param-name>cors.exposed.headers</param-name>
        <param-value>Access-Control-Allow-Origin,Access-Control-Allow-Credentials</param-value>
    </init-param>
    <init-param>
        <param-name>cors.support.credentials</param-name>
        <param-value>false</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>CorsFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

0

以下是Tomcat CORS过滤器中的代码,使用"file://"作为源进行新URI的调用会导致URISyntaxException错误,并返回403。

protected static boolean isValidOrigin(String origin) { URI originURI; try { originURI = new URI(origin); } catch (URISyntaxException e) { return false; } return originURI.getScheme() != null; }

有人在以下链接中试图覆盖此过滤器。 https://github.com/sebastienblanc/cors-filter/blob/master/src/main/java/org/ebaysf/web/cors/CORSFilter.java#L825


0
我在我的服务器上更改了Tomcat过滤器。 我在自定义过滤器中修改了请求的Origin标头,它起作用了。我不知道这是否是正确的做法,但这是唯一让它工作的方法。

你能否提供一个示例,说明你是如何修改它的? - Andrew Kovalenko
1
这是我在服务器过滤器中添加的解决方法,用于处理 file:// 原始头:如果 (origin == null || origin.isEmpty() || origin.equalsIgnoreCase("file://")) { request.addHeader("Origin", "http://localhost:9090"); } - Bil

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接