AJAX请求从PhoneGap的Andr​​oid的失败 [英] AJAX Request from Phonegap Android fails

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

问题描述

我一直在这方面的工作在过去的两天,看着很多其他建议。是的,我能得到这个简单的Ajax请求,从一个PhoneGap的应用程序的工作,无论是在Android模拟器,并在实际的android手机。

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

在code我使用的是:

  VAR URL ='http://www.thomas-bayer.com/sqlrest/CUSTOMER';
    返回$阿贾克斯({
        键入:GET,
        网址:网址,
        超时:60 * 1000
    })。完成(功能(数据){
        警报('嗨');
    }),失败(函数(A,B,C){
        的console.log(B +'|'+ C);
    });
 

结果我收到在日志中就是:

  

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

我添加的设置到AndroidManifest.xml中

 <使用-权限的Andr​​oid:名称=android.permission.INTERNET对/>
<使用-权限的Andr​​oid:名称=android.permission.ACCESS_NETWORK_STATE/>
<使用-权限的Andr​​oid:名称=android.permission.READ_PHONE_STATE/>
 

和我在Config.xml中的以下

 < PARAM NAME =Android的包值=org.apache.cordova.core.NetworkManager/>
 

当我检查 navigator.connection.type 我得到 3G 在模拟器和无线物理电话。

任何想法,还有什么能去哪里呢?

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

  {的readyState:4,状态:404,状态文本:错误}
 

解决方案

您应该白名单,以便域为您的AJAX调用工作。

这行添加到配置文件 -

 <获得原产地=*/>
 

的PhoneGap的默认的策略阻塞,除非另有规定所有网络访问。上面一行将禁用此安全限制。您也可以更具体,只允许某些领域通过在像这样的配置文件域名绕过这个安全功能

 <获得原产地=HTTP:// YOURDOMAIN/>
 

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.

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

The code I'm using is:

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:

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

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" />

and I have the following in Config.xml

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

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?

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

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

解决方案

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

Add this line to config file -:

<access origin="*" />

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" />

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

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