【问题标题】:Cannot send cross domain requests with Cordova. Whitelisting doesn't work无法使用 Cordova 发送跨域请求。白名单不起作用
【发布时间】:2015-01-16 12:32:29
【问题描述】:

我无法使用 Cordova 发出跨域请求。花了几个小时在这上面,仍然不确定出了什么问题。也许有人处理过这样的问题?谢谢!

.js 文件:

//works fine, test.html - local file
  $.get("test.html",function(data,status){
    alert("Data: " + data + "\nStatus: " + status);
  });

//does not do anything
  $.get("http://www.stackoverflow.com",function(data,status){
    alert("Data: " + data + "\nStatus: " + status);
  });

项目配置文件:

..
<access origin="stackoverflow.com"/>
..

也试过了:

 <access origin="www.stackoverflow.com"/>
 <access origin="http://www.stackoverflow.com"/>
 <access origin="*"/>

AndroidManifest.xml:

..
<uses-permission android:name="android.permission.INTERNET" />
..

【问题讨论】:

    标签: android cordova whitelist


    【解决方案1】:

    我遇到了同样的问题......经过一些研究,我找到了一种方法来做到这一点。 使用 jQuery $.ajax 发出“GET”请求。
    (实际上,我试图访问 SOAP WebService,然后我发现这种方式可以使用 POST 或 GET 请求)
    JSFiddle 中进行测试。
    不过,您仍然需要访问源...

    Obs:您的请求将不起作用,除非您尝试获取的页面允许您这样做。示例页面允许访问,所以...尝试从您的 Cordova 应用程序访问它。

    $.ajax({
                type: 'GET',
                url: "http://anytime.ueuo.com/http-return.php",
                crossDomain: true,
                success: function (data, textStatus, jqXHR) {
                    alert("Ok!");
                    $("#retorno").html(data);
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    alert("Wait, take a look: " + textStatus + ", " + errorThrown);
                },
                complete: function (jqXHR, textStatus ) {
                    alert("Status: " + textStatus);
                }
            });
    

    Obs-2:您的代码返回错误: XMLHttpRequest 无法加载 http://www.stackoverflow.com/请求的资源上不存在“Access-Control-Allow-Origin”标头。因此不允许访问源“http://localhost:8383”。 (02:18:52:813 | 错误,javascript)位于 public_html/index.html。这就是为什么您需要使用预先配置的页面进行测试的原因。

    对不起,我的英语不好'-'

    【讨论】:

      猜你喜欢
      • 2012-06-13
      • 2015-04-15
      • 2016-12-01
      • 2015-04-22
      • 1970-01-01
      • 2015-01-22
      • 2016-10-02
      • 2014-04-18
      • 1970-01-01
      相关资源
      最近更新 更多