PhoneGap的不发出AJAX(JSONP)要求 [英] PhoneGap not issuing AJAX (jsonp) request
问题描述
我一直在试图整天战这个问题,用Google搜索所有的地方。这可能是因为我无法找到一个页面的解决方案...但我看了看问题和解决方案,如:
<一个href="http://stackoverflow.com/questions/6518278/jquery-mobile-phonegap-ajax-calls-not-working-on-android-emulator?rq=1">JQuery手机+ PhoneGap的:Ajax调用不工作在Android模拟器
和既不是解决方案的出现似乎已经解决了我的问题。
我想要做的是发出一个AJAX调用我的远程服务器,上传了一些参数和下载一些内容。该脚本完全在浏览器中,但根据小的改动在PhoneGap的文件,无论是抛回一个超时的错误,或者只是简单地说:错误。在code是如下(AJAX端点是一个开发终点,我会离开它,所以你可以尝试打它):
&LT; HTML&GT;
&LT; HEAD&GT;
&LT;脚本类型=文/ JavaScript的字符集=utf-8SRC =jQuery的-1.10.2.min.js&GT;&LT; / SCRIPT&GT;
&LT; /头&GT;
&LT;身体GT;
&LT; UL&GT;
&LT; DIV ID =应用程序状态-UL&GT;
&LT;李&GT;加载&LT; /李&GT;
&LT; / DIV&GT;
&LT; / UL&GT;
&LT; /身体GT;
&LT;脚本&GT;
VAR serverRegHit =功能(数据,httpStat){
$(#应用程序状态-UL)追加('&LT;李&GT;服务器注册OK,HTTP状态:+ httpStat +'&LT; /李&GT;');
$(#应用程序状态-UL)追加('&LT;李&GT;数据状态:+ JSON.stringify(数据)+'&LT; /李&GT;');
};
VAR serverRegFail =功能(数据,httpStat){
$(#应用程序状态-UL)追加('&LT;李&GT;服务器注册失败,HTTP状态:+ httpStat +'&LT; /李&GT;');
$(#应用程序状态-UL)追加('&LT;李&GT;数据状态:+ JSON.stringify(数据)+'&LT; /李&GT;');
};
$阿贾克斯(网址:{url:http://keb.bz/gcm/?deviceid=12345
JSONP:'回调',
数据类型:JSONP,
超时:25000,
成功:serverRegHit,
错误:serverRegFail});
&LT; / SCRIPT&GT;
&LT; / HTML&GT;
我用科尔多瓦2.9都试过了,降级到2.5,但是这似乎并没有帮助任何。
我的config.xml中已列入白名单*和keb.bz域的权限给到互联网的应用程序访问的清单 - 我认为这工作,因为它能够成功地检索GCM设备ID 。 P>
我通过AJAX使用JSON,JSONP,TEXT,以及不同数据类型的的都试过了,但这些都没有什么不同。
有科尔多瓦/ PhoneGap的另一个配置来看,我很想念?
在这里的任何帮助将是AP preciated。
感谢@xioawl让我仔细检查我的manifest.xml。原来我没有正确配置的权限存在。
在最后,我结束了使用科尔多瓦2.5,jQuery的1.10,(没有使用jQuery Mobile的),我的设置RES / config.xml中具有以下存取路径:
&LT;获得原产地=HTTP://127.0.0.1*/&GT;
&LT;获得原产地=http://keb.bz子域=真/&GT;
&LT;*获得原产地= /&GT; &LT;! - 只在开发过程 - &GT;
包括所有的JavaScript库后,放置这段代码:
&LT;脚本&GT;
(文档)$ .bind(mobileinit,函数(){
$ .mobile.allowCrossDomainPages = TRUE;
});
&LT; / SCRIPT&GT;
和使用下面的manifest.xml权限:
&LT;使用-权限的Android:名称=android.permissions.INTERNET/&GT;
&LT;使用-权限的Android:名称=android.permissions.NETWORK_ACCESS/&GT;
&LT;使用-权限的Android:名称=android.permissions.ACCESS_NETWORK_STATE/&GT;
我的JSONP查询仍然如下所示:
$阿贾克斯(网址:{url:http://keb.bz/gcm/?deviceid=12345
JSONP:'回调',
数据类型:JSONP,
超时:25000,
成功:serverRegHit,
错误:serverRegFail});
希望这是有用的人!
I've been trying to battle this issue all day, and have googled all over the place. It may be that I just can't find that one page with the solution... but I have looked at problems and solutions such as:
Ajax call on phonegap not sending request
JQuery mobile + Phonegap : Ajax calls not working on Android Emulator
and neither of the solutions there appeared to have resolved my issue.
All I want to do is issue an AJAX call to my remote server, upload a few parameters and download some content. This script works perfectly in a browser, but depending on minor changes to the file in Phonegap, either throws back a "timeout" error, or simply just says "error". The code is as follows (the AJAX endpoint is a dev endpoint, and I'll leave it up so you can try hitting it as well):
<html>
<head>
<script type="text/javascript" charset="utf-8" src="jquery-1.10.2.min.js"></script>
</head>
<body>
<ul>
<div id="app-status-ul">
<li>Loaded</li>
</div>
</ul>
</body>
<script>
var serverRegHit = function(data, httpStat){
$("#app-status-ul").append('<li>Server Reg OK, HTTP status: '+httpStat+'</li>');
$("#app-status-ul").append('<li>Data Status: '+JSON.stringify(data)+'</li>');
};
var serverRegFail = function(data, httpStat){
$("#app-status-ul").append('<li>Server Reg FAIL, HTTP status: '+httpStat+'</li>');
$("#app-status-ul").append('<li>Data Status: '+JSON.stringify(data)+'</li>');
};
$.ajax({url:"http://keb.bz/gcm/?deviceid=12345",
jsonp: 'callback',
dataType: 'jsonp',
timeout: 25000,
success: serverRegHit,
error: serverRegFail});
</script>
</html>
I have tried using cordova 2.9, and downgraded to 2.5, but this does not seem to have helped either.
My config.xml has whitelisted .* and the keb.bz domain, the permissions give the app access to the internet in the manifest - and I assume this works because it is able to successfully retrieve a GCM Device ID.
I have tried using JSON, JSONp, TEXT, and various dataType's through AJAX, but none of these have made any difference.
Is there another configuration point in Cordova/PhoneGap that I am missing?
Any help here would be appreciated.
Thanks to @xioawl for making me double check my manifest.xml. Turns out I had not correctly configured permissions there.
In the end, I wound up using Cordova 2.5, jQuery 1.10, (did not use jQuery Mobile), setting my res/config.xml with the following access paths:
<access origin="http://127.0.0.1*" />
<access origin="http://keb.bz" subdomains="true" />
<access origin=".*" /> <!-- only during development -->
Placing this snippet after including all JavaScript libraries:
<script>
$(document).bind("mobileinit", function(){
$.mobile.allowCrossDomainPages = true;
});
</script>
And using the following manifest.xml permissions:
<uses-permission android:name="android.permissions.INTERNET" />
<uses-permission android:name="android.permissions.NETWORK_ACCESS" />
<uses-permission android:name="android.permissions.ACCESS_NETWORK_STATE" />
My JSONp Query continues to look as follows:
$.ajax({url:"http://keb.bz/gcm/?deviceid=12345",
jsonp: 'callback',
dataType: 'jsonp',
timeout: 25000,
success: serverRegHit,
error: serverRegFail});
Hope this is useful to someone!
这篇关于PhoneGap的不发出AJAX(JSONP)要求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!