PhoneGap的不发出AJAX(JSONP)要求PhoneGap、AJAX、JSONP

2023-09-11 00:34:46 作者:卮言

我一直在试图整天战这个问题,用Google搜索所有的地方。这可能是因为我无法找到一个页面的解决方案...但我看了看问题和解决方案,如:

的Ajax调用的PhoneGap不发送申请

JQuery手机+ PhoneGap的:Ajax调用不工作在Android模拟器

和既不是解决方案的出现似乎已经解决了我的问题。

我想要做的是发出一个AJAX调用我的远程服务器,上传了一些参数和下载一些内容。该脚本完全在浏览器中,但根据小的改动在PhoneGap的文件,无论是抛回一个超时的错误,或者只是简单地说:错误。在code是如下(AJAX端点是一个开发终点,我会离开它,所以你可以尝试打它):

 < HTML>
< HEAD>
<脚本类型=文/ JavaScript的字符集=utf-8SRC =jQuery的-1.10.2.min.js>< / SCRIPT>
< /头>

<身体GT;
< UL>
< D​​IV ID =应用程序状态-UL>
<李>加载< /李>
< / DIV>
< / UL>
< /身体GT;
<脚本>
VAR serverRegHit =功能(数据,httpStat){
    $(#应用程序状态-UL)追加('<李>服务器注册OK,HTTP状态:+ httpStat +'< /李>');
    $(#应用程序状态-UL)追加('<李>数据状态:+ JSON.stringify(数据)+'< /李>');
};
VAR serverRegFail =功能(数据,httpStat){
    $(#应用程序状态-UL)追加('<李>服务器注册失败,HTTP状态:+ httpStat +'< /李>');
    $(#应用程序状态-UL)追加('<李>数据状态:+ JSON.stringify(数据)+'< /李>');
};
      $阿贾克斯(网址:{url:http://keb.bz/gcm/?deviceid=12345
              JSONP:'回调',
              数据类型:JSONP,
              超时:25000,
              成功:serverRegHit,
              错误:serverRegFail});

< / SCRIPT>
< / HTML>
 
ajax异域请求jsonp, ajax 异源请求jsonp

我用科尔多瓦2.9都试过了,降级到2.5,但是这似乎并没有帮助任何。

我的config.xml中已列入白名单*和keb.bz域的权限给到互联网的应用程序访问的清单 - 我认为这工作,因为它能够成功地检索GCM设备ID

我通过AJAX使用JSON,JSONP,TEXT,以及不同数据类型的的都试过了,但这些都没有什么不同。

有科尔多瓦/ PhoneGap的另一个配置来看,我很想念?

在这里的任何帮助将是AP preciated。

解决方案

感谢@xioawl让我仔细检查我的manifest.xml。原来我没有正确配置的权限存在。

在最后,我结束了使用科尔多瓦2.5,jQuery的1.10,(没有使用jQuery Mobile的),我的设置RES / config.xml中具有以下存取路径:

 <获得原产地=HTTP://127.0.0.1*/>
<获得原产地=htt​​p://keb.bz子域=真/>
<*获得原产地= /> <! - 只在开发过程 - >
 

包括所有的JavaScript库后,放置这段代码:

 <脚本>
(文档)$ .bind(mobileinit,函数(){
   $ .mobile.allowCrossDomainPages = TRUE;
});
< / SCRIPT>
 

和使用下面的manifest.xml权限:

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

我的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!