local-host 2017-05-02 16:47 采纳率: 100%
浏览 110

CORS在Cordova中重定向

I have a Cordova/Phonegap application trying to get some files with AJAX. I get the following error:

XMLHttpRequest cannot load https://docs.google.com/uc?export=open&id=.... Redirect from 'https://docs.google.com/uc?export=open&id=...' to 'https://doc-xx-xx-docs.googleusercontent.com/docs/...?e=open' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.

I tried adding the following to config.xml

<access origin="*" />
<allow-navigation href="*" />
<allow-navigation href="http://*/*" />
<allow-navigation href="https://*/*" />

and also added to index.html

<meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: 'unsafe-eval' 'unsafe-inline' http: https:">

I'm accessing the application in the browser using cordova serve.

I still get the same error. Any idea?

  • 写回答

2条回答 默认 最新

  • weixin_33739646 2017-05-02 17:07
    关注

    Assuming you don't​ have control over the endpoint you may well need to setup your own API to do this.

    For example if you create an endpoint in php you could include the line:

     header('Access-Control-Allow-Origin: *');
    

    Then your end point can forward the request to the Google endpoint which will pass the response to your API and that can pass your app the response.

    I believe the Google endpoint will accept a request from other servers but not if the origin is null.

    In effect you need to make sure your request is coming from a http:// and not a file:// URL.

    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测