【问题标题】:Ajax call with cordova whitelist error带有科尔多瓦白名单错误的 Ajax 调用
【发布时间】:2016-06-02 10:57:13
【问题描述】:

我看过很多关于实现cordova白名单插件的帖子,但经过整整一周的测试,我仍然没有发现我的错误。

此 ajax 调用会引发以下警报:

xhr {"readystate":0,"status":0,"statustext":"error"}。

状态“错误”

错误“”

$.ajax({
    url: 'http://www.example.com/my_file.php',
    data: {type: 'test', code: '11'},
    method: "GET",
    dataType: "json",
    timeout: 5000,
    success: function (data) {
        alert('done '+JSON.stringify(data));
    },
    error: function (xhr, status, error) {
            alert('xhr '+JSON.stringify(xhr));
            alert('status'+JSON.stringify(status));
            alert('error '+JSON.stringify(error));
    }
});

我使用新的 cordova 白名单实现更新了我的 phonegap 构建应用程序,并将其添加到元:

<meta http-equiv="Content-Security-Policy" content="default-src data: gap: https://ssl.gstatic.com 'unsafe-eval' *; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline' 'unsafe-eval' http://www.example.com; connect-src 'self' http://www.example.com">

这个到config.xml:

<gap:plugin name="cordova-plugin-whitelist" source="npm"/>
<allow-navigation href="*" />
<allow-intent href="*" />
<access origin="*" />

在 ajax 调用之前的 js 中:

$.support.cors=true;

我目前正在使用所有通配符进行测试,稍后我会更改。这是我正在调用的服务器 php 文件:

<?php 

header("Access-Control-Allow-Origin: *");
header('Content-Type: application/json');

$data = json_encode(array($_GET));

echo $data;

?>

Phonegap 构建版本 cli-5.2.0 安卓 4.1.1 任何帮助将不胜感激

【问题讨论】:

  • 您的两个代码都可以正常工作。确保您的网址正确,因为这可能是个问题。你可以试试alert(error); 看看你实际上遇到了什么错误
  • 如果在浏览器中调用,完全相同的请求会起作用,因此它不能是 URL,我猜它应该与科尔多瓦白名单有关。我编辑帖子以包含错误并澄清问题。

标签: ajax cordova whitelist


【解决方案1】:

在您的 config.xml 中试试这个

<access origin="*"/>
<access origin="tel:*" launch-external="yes"/>
<allow-navigation href="*"/>
<allow-navigation href="http://*/*"/>
<allow-navigation href="https://*/*"/>
<allow-navigation href="data:*"/>
<allow-intent href="*"/>
<access origin="*"/>

【讨论】:

  • 我试过了,还是报同样的错误,你是不是故意写了两次
猜你喜欢
  • 2017-05-20
  • 1970-01-01
  • 2015-09-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多