加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angularjs – 手机支持Cors吗?

发布时间:2020-12-17 17:13:35 所属栏目:安全 来源:网络整理
导读:我是phonegap的新手.我想在phonegap中运行一个AngularJs应用程序.在服务器端我使用Apache tomcat 7 CORS过滤器和Rest.当我从浏览器运行应用程序时它工作正常.但是目前我在ios或 android中使用phonegap运行应用程序,GET请求正常,但是post请求提供了403 Forbid
我是phonegap的新手.我想在phonegap中运行一个AngularJs应用程序.在服务器端我使用Apache tomcat 7 CORS过滤器和Rest.当我从浏览器运行应用程序时它工作正常.但是目前我在ios或 android中使用phonegap运行应用程序,GET请求正常,但是post请求提供了403 Forbidden响应.我可以在请求“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>

这是我从客户发布请求时得到的结果:

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中添加以下行,还可以在angular.js中启用CORS:

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

如果以前有人遇到过这个问题,请帮我解决这个问题.

解决方法

是的,您需要做的就是在config.xml文件中添加以下行:

<access origin="http://example.com" />

你也可以这样做:

<access origin="*" />

但是,指定要发送请求的域更安全.

如果您需要更多信息,请在PhoneGap doc上查看此page.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读