【问题标题】:Android + JQM+ jsonp not working consistentlyAndroid + JQM+ jsonp 无法始终如一地工作
【发布时间】:2012-07-09 18:24:24
【问题描述】:

我正在使用 JQM + PhoneGap 开发 Hybrid Android App 我正在使用 AJAX 进行 JSONP 请求

它适用于所有 Chrome PC 浏览器,在连接 WiFi 时也适用于我的 android 应用程序, 但是将网络连接更改为 3G 时,AJAX 请求没有响应。

我发现@BruceHill 相关的帖子写了以下内容: “移动运营商在将内容发送到手机之前会进行内容修改,这种修改会破坏 jQuery” Jquery mobile page not loading correctly on Netherlands 3G connections

虽然我不住在荷兰,但我尝试按照他的建议将所有 JS 文件定位在远程服务器上并通过 CDN 调用它,但不幸的是它没有帮助。

我很乐意在这方面得到一些帮助......

这是我的 AJAX 请求:

$.mobile.allowCrossDomainPages = true;

$('#expertsListPage').live('pageshow', function (event) {
   $.mobile.showPageLoadingMsg(); 
    getExpertsList();
});


var catId;
var catName
function getExpertsList() {


    $('#expertsList li').remove();
    catId = getUrlVars()["id"];
    catName = getUrlVars()["cat"]  ;

    $('h1').text( unescape(catName) );

    var url = 'http://apis.mydomain.com/mydata.svc/jsonp' 


    $.ajax({
         cache: true,
         type: 'GET',
         url: url,
         dataType: 'jsonp' ,
         jsonp: 'callback',
         success:api_do
    });


}

var expertss;
function api_do(obj) {

    $('#expertsList li').remove();

    expertss = obj.experts;

    $.each(expertss, function (index, expert) {

        $('#expertsList').append('<li><a  href="ExpertDetails.html?id=' + expert.id + '&catid=' + catId +'&catName=' + catName + '">' +
                    '<img style="width:160px;height:160px;" src="' + expert.thumbnail + '"/>' +
                    '<h4>' + expert.name + '</h4>' +
                    '<p>' + expert.description + '</p>' +
                    '</a></li>');

    });

    $('#expertsList').listview('refresh');

    $.mobile.hidePageLoadingMsg();
}


function getUrlVars() {
    var varsExperts = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for (var i = 0; i < hashes.length; i++) {
        hash = hashes[i].split('=');
        varsExperts.push(hash[0]);
        varsExperts[hash[0]] = hash[1];
    }
    return varsExperts;
}

【问题讨论】:

  • 您可以使用 PhoneGap 的控制台日志记录功能打印服务器响应字符串并分析 wifi 和 3G 的输出 - 这将提供 wifi 通信和 3G 通信之间差异(如果有)的详细信息。服务器端日志也有助于识别服务器收到的请求
  • 您的服务返回什么样的数据? 3G 比 WiFi 慢,你可能只是超时了。你说“AJAX 请求没有响应”——你实际上得到了什么错误?添加错误回调以查看错误是什么。
  • @Makhijani 不幸的是,我在 API 服务器上看不到错误,因为我没有远程服务器的权限。
  • @codemonkey 数据大小在 50KB 左右,这是 Eclipce logcat link
  • 50K over 3G,我猜这是超时。 logcat 里面没有太多内容。向您的 $.ajax 调用添加错误函数 - error(jqXHR, textStatus, errorThrown)。这为您提供了有关错误的详细信息。很可能 textStatus 将超时。如果是这种情况,您可以增加超时时间或尝试压缩数据。

标签: android jquery ajax cordova


【解决方案1】:

尝试将此代码添加到您的 javascript,可能会有所帮助。

$( document ).on( "mobileinit", function() {
                 // Make your jQuery Mobile framework configuration changes here!
    $.support.cors = true;
    $.mobile.allowCrossDomainPages = true;
});

【讨论】:

    猜你喜欢
    • 2023-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-10
    • 2018-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多