3

我对 android 还很陌生,我遇到了一个问题,即带有 ajax 调用的页面没有加载到使用 Phonegap 开发的本机应用程序中。

跨域AJAX POST请求在 Web 浏览器(包括手机浏览器)上运行良好,但不适用于使用 Phonegap 构建的本机应用程序。

我创建了一个应用程序durandal,从中获取数据Facebook并在页面中显示。它在浏览器中运行良好,但使用 ajax 调用的页面未加载到使用Phonegap. 注意:其他静态页面工作正常。

我的 Ajax 脚本:

define(function(require){
  return {
    getCustomers:function(){
      //do some ajax and return a promise
        return $.ajax({
          url: 'http://graph.facebook.com/facebook?callback=?',
          dataType: 'json',
        }).promise();
    }
  };
});

域白名单 - config.xml

<access origin="*"/>
<!-- <content src="http://mysite.com/myapp.html" /> for external pages -->
<content src="index.html" />

用户权限

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

我有一个错误。不知道跟这个有关系吗?

TypeError: Result of expression 'parentElement' [null] is not an object. at file:///android_asset/www/js/index.js:41
4

3 回答 3

1

CORS parameter must be enabled in your server end as well, along with passing the "mobile.allowCrossDomainPages = true;" in phonegap.

search for CORS related quetions in stackoverflow, there are many solutions.

Let me explain why it works in mobile browser but not on phonegap packaging. Phonegap packaging pulls the files from the application's local file system, making the reference domain as "Local". When you make a request to the URL, it is apparently in a different domain, hence the CORS. Enabling CORS parameter in phonegap configuration mostly solves the issue. In case it pertains, you must check that the webservice/URL supports the CORS. Look for server side CORS parameter enabling.

于 2013-11-11T05:41:52.700 回答
0

你确定清楚地为你的项目设置了phonegap吗?

请查看此页面: http: //www.adobe.com/devnet/html5/articles/getting-started-with-phonegap-in-eclipse-for-android.html并逐步检查。

于 2013-11-05T23:00:18.213 回答
0

它应该工作。无论如何,尝试将此添加到头部代码中,

$(document).bind("mobileinit", function () {
    $.support.cors = true;
    $.mobile.allowCrossDomainPages = true;
});

并尝试删除,这里

    return $.ajax({
      url: 'http://graph.facebook.com/facebook?callback=?',
      dataType: 'json' //<<---HERE
    }).promise();

是吗jsonjsonp

于 2013-11-06T16:31:15.867 回答