CORS + Android Webview,在模拟器上可以运行但在实际设备上无法工作

4

我有一个工作中的HelloWorld phonegap程序,其中涵盖了jquery mobile,具体描述可以参考此处:http://jquerymobile.com/demos/1.1.0/docs/about/getting-started.html。我在其中加入了一些JavaScript来尝试跨域资源共享:

<script>
$(document).bind("pageinit", function() {
    $.support.cors = true;
    $.mobile.allowCrossDomainPages = true;
    $.mobile.changePage("http://jquery.com");
});
</script>

在模拟器上(2.3版本),jquery.com可以正常加载jquery mobile演示。然而,在实际的2.3安卓设备上(运行Cyanogen的T-mobile G2、Galaxy SII和Galaxy Player),changePage()调用没有任何反应。


你的缓存和异步字段设置为什么? - Erol
2个回答

4
在`pageinit`函数内调用`$.mobile.changePage()`函数听起来像是个坏主意,因为这会导致无限循环。`$.mobile.changePage()`函数初始化了指定为`target`参数的页面,所以每次调用`$.mobile.changePage()`时,也会触发一个`pageinit`事件。
你可能想要绑定到`mobileinit`事件来覆盖`jQuery Mobile`初始化前的`$.support.cors`变量。
<script src="jquery.js"></script>
<script>
$(document).bind("mobileinit", function() {
    $.support.cors = true;
    $.mobile.allowCrossDomainPages = true;
    $.mobile.changePage("http://jquery.com");
});
</script>
<script src="jquery-mobile.js"></script>

相关文档:

这里提供了两个关于JQuery Mobile的文档链接,分别是全局配置和事件。你可以通过这些文档了解更多关于如何使用JQuery Mobile的信息。

1
尝试使用mobileinit代替pageinit。因为您绑定的事件是普通的jQuery事件,而对于jQuery移动设备,初始化事件是mobileinit。 $.mobile.allowCrossDomainPages选项必须在进行任何跨域请求之前设置,因此我们建议将其包装在mobileinit处理程序中。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接