Phonegap和jquery mobile:a href - > Access-Control-Allow-Origin不允许使用null

时间:2011-12-11 12:19:13

标签: jquery-mobile cordova

我试图在多页文档中使用带有phonegap的jquery mobile。 要在文档中使用基本的 href 链接,给出 Origin null,Access-Control-Allow-Origin 错误是不允许的,这非常烦人。

这是因为索引页面是通过file://而不是http://引用的,其中webkit将其解释为origin null。有没有人有jquery移动和phonegap在多页面环境中工作?如果是这样你怎么能这样做?如果在href标签中添加rel = external,则链接可以正常工作,但当然所有转换都会丢失。

无法在堆栈溢出或teh internetz上找到有关此特定问题的任何信息。

<!DOCTYPE HTML>
<html>

<head>
<title>PhoneGap</title>

<script type="text/javascript" charset="utf-8" src="phonegap-1.2.0.js"></script>   
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.0/jquery.mobile-1.0.min.css" />
<script src="http://code.jquery.com/jquery-1.6.4.min.js"></script>
<script>
    $(document).bind( "mobileinit", function(){
        //alert("mobileinit fired");  
        $.support.cors = true;
        $.mobile.allowCrossDomainPages = true;       
    });        
</script>   
<script src="http://code.jquery.com/mobile/1.0/jquery.mobile-1.0.min.js"></script>

<script type="text/javascript">

function onDeviceReady() {
    navigator.network.isReachable("google.com", reachableCallback, {});
}
// Check network status
function reachableCallback(reachability) {
    // There is no consistency on the format of reachability
    var networkState = reachability.code || reachability;
    var states = {};
    states[NetworkStatus.NOT_REACHABLE]                      = 'No network connection';
    states[NetworkStatus.REACHABLE_VIA_CARRIER_DATA_NETWORK] = 'Carrier data connection';
    states[NetworkStatus.REACHABLE_VIA_WIFI_NETWORK]         = 'WiFi connection';
    if (networkState != 0) online = true;
}
var online = navigator.onLine || false;

$(document).ready(function() { 
    $(document).bind('deviceready', function(){
        onDeviceReady()
})
// Your main code
})
//Now if you about to make an AJAX call to load up some dynamic data, you can easily check to see if you're online
if(online) {
    } else {
}


</script>

</head>

    <body>
        <h1>Welcome to PhoneGap</h1>
    <a href="edit.html">Edit html</a>
    </body>

</html>

5 个答案:

答案 0 :(得分:7)

这是official documentation关于如何做你想要的......

希望这有帮助!

答案 1 :(得分:5)

Leon的评论是正确答案 - 您需要将rel="external"添加到静态链接。

答案 2 :(得分:1)

测试

  1. 下载mongoose http server
  2. 将mongoose_xxxxxxx.exe文件复制到您的assets / www
  3. 现在,您可以在没有Access-Control-Allow-Origin
  4. 的情况下为jquery mobile设计html页面

答案 3 :(得分:0)

我认为你可以在这里找到解决方案:http://view.jquerymobile.com/master/demos/faq/how-configure-phonegap-cordova.php

$.mobile.allowCrossDomainPages = true; $.support.cors = true; $.mobile.phonegapNavigationEnabled = true

虽然我还没有开始工作,但我认为这是解决方案。

答案 4 :(得分:0)

如果您将目标定位在JELLY_BEAN(API级别16)之上,则可以添加到MainActivity类中。


if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) { super.appView.getSettings().setAllowUniversalAccessFromFileURLs(true); }

这将允许空来源XHR请求。