我已经处理这个问题三天了,我已经尝试了几乎所有的替代代码和修复程序,但都无济于事。
这是代码,取自沼泽标准 jQuery 示例:
$.ajax({
url: "http://api.wunderground.com/api/4213a68e7f20c998/geolookup/conditions/forecast/q/Australia/Noarlunga.json",
dataType: "jsonp",
success: function(parsed_json) {
alert("YEP!");
var loc = parsed_json['response'];
var weather = "Location: " + parsed_json.location.city + "<br />";
weather += "Wind: " + parsed_json.current_observation.wind_dir + " ";
weather += parsed_json.current_observation.wind_mph + "-" + parsed_json.current_observation.wind_gust_mph + " knts";
$("#info").html(weather);
}
});
这是很多人建议的一些配置(什么也没做)
$( document ).bind( "mobileinit", function() {
// Make your jQuery Mobile framework configuration changes here!
$.mobile.allowCrossDomainPages = true;
$.mobile.ajaxEnabled = true;
$.mobile.pushStateEnabled = false;
$.mobile.allowCrossDomainPages = true;
});
此外,我还有:
在 AndroidManifest.xml 文件中添加了
在同一目标的浏览器中测试了 json 请求 URL(工作正常)
- 在 FireFox/Chrome 中测试了页面(工作正常)
- 通过 3G 在我的 Galaxy s2 上试用了该应用程序(用户界面一切正常,但 ajax 请求仍然失败)
请注意,这不是完整的代码,它只是包含在标准 .ready() 函数中的标准 phonegap deviceready 监听器事件函数中。
事实上这在我的手机和 SDK 上失败似乎表明存在 phonegap/Eclipse 问题,而不是连接/权限问题。
我正在尝试使用 Android SDK 部署到 Android 2.3 AVD。我还没有尝试部署到 Android 4 AVD。
如果有人知道问题出在哪里,我会很高兴,因为我已经没有足够的建议可以尝试了!
最佳答案
前提是你可以修改服务器上的php代码..
试试这个,这样你就可以在没有 JSONP 的情况下在本地浏览器或移动设备上进行测试
1.按照 jQueryMobile 网站的说明进行操作,该网站很好地解释了如何使其与 phonegap (http://jquerymobile.com/test/docs/pages/phonegap.html) 一起工作
在你的<script></script>
区域,添加
$(document).on("mobileinit", function(){
//apply overrides here
$.mobile.allowCrossDomainPages = true;
$.support.cors = true;
});
你的 ajax 可能是
$.ajax({
type: "GET",
url: "http://xx.xx.xx.xx/xxx/xx.php"
}).done(function( msg ) {
alert(msg);
//refresh the content
$( '.ui-content' ).load( 'xx/xx.html', function () {$(this).trigger('create') });
});
2.你的php是这样的:
<?php
header('Access-Control-Allow-Origin: *');//for local browser test
$test='[{"name":"aa","address":"aa"}, {"name":"bb","address":"bb"}]';
echo $test;
?>
关于android - phonegap jQuery .ajax 跨域请求在浏览器中工作,在 Android SDK 中失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11641442/