来自 Phonegap Android 的 AJAX 请求失败 [英] AJAX Request from Phonegap Android fails

查看:27
本文介绍了来自 Phonegap Android 的 AJAX 请求失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

过去两天我一直在研究这个问题,并查看了很多其他建议.是的,我可以让这个简单的 ajax 请求在 phonegap 应用程序中工作,无论是在 android 模拟器还是在实际的 android 手机上.

I have been working on this for the last two days, and looking at a lot of other suggestions. Yes I can get this simple ajax request to work from within a phonegap application, both on the android emulator and on an actual android phone.

我的 phonegap 版本是(使用 phonegap -v)3.0.0-0.14.3

My phonegap version is (using phonegap -v) 3.0.0-0.14.3

我使用的代码是:

var url = 'http://www.thomas-bayer.com/sqlrest/CUSTOMER';
    return $.ajax({
        type: "GET",
        url: url,
        timeout: 60 * 1000
    }).done(function (data) {
        alert('hey');
    }).fail(function (a, b, c) {
        console.log(b + '|' + c);
    });

我在日志中得到的结果只是:

The result I'm getting in the log is just:

错误|在 file:///android_asset/www/js/index.js:62

error| at file:///android_asset/www/js/index.js:62

我将设置添加到 AndroidManifest.xml

I added the settings to the AndroidManifest.xml

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

我在 Config.xml 中有以下内容

and I have the following in Config.xml

<param name="android-package" value="org.apache.cordova.core.NetworkManager" />

当我检查 navigator.connection.type 时,我在模拟器上得到 3G,在实体手机上得到 wifi.

When I check navigator.connection.type I get 3G on the emulator and wifi on the physical phone.

知道还有什么地方会出错吗?

Any idea what else could go wrong?

更新:如果我在失败函数的第一个参数中记录 JSON,我会得到:

UPDATE: If I log the JSON in the first parameter of the failing function I get:

{"readyState":4,"status":404,"statusText":"error"}

推荐答案

你应该 白名单 域,以便您的 AJAX 调用正常工作.

You should whitelist the domain in order for your AJAX calls to work.

将此行添加到配置文件 -:

Add this line to config file -:

<access origin="*" />

除非另有说明,Phonegap 的默认策略会阻止所有网络访问.上面的行将禁用此安全限制.您还可以通过在配置文件中包含域名来更具体地只允许某些域绕过此安全功能

Phonegap's default policy blocks all network access unless specified otherwise. The above line will disable this security restriction. You can also be more specific in allowing only certain domains to bypass this security feature by including the domain name in the config file like so

<access origin="http://yourdomain" />

这篇关于来自 Phonegap Android 的 AJAX 请求失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆