簡體   English   中英

Android Cordova2.3 -jquery ajax($。support.cors = true)不起作用(相同的請求在chrome瀏覽器上有效)

[英]Android Cordova2.3 -jquery ajax($.support.cors = true) not working(same request works on chrome browser)

我已經閱讀了有關此問題的信息,並在線嘗試了所有可能的解決方案,但沒有任何效果。這是我的html代碼...

<!DOCTYPE html> 
<head>
<link rel="stylesheet" href="jquery.mobile-1.2.0.min.css" />
<script src="jquery-1.8.2.min.js"></script>
<script>

$( document ).bind( "mobileinit", function() {
    // Make your jQuery Mobile framework configuration changes here!
    console.log("made cors changes");
    //$.mobile.ajaxEnabled = true;
    $.support.cors = true;
    $.mobile.allowCrossDomainPages = true;

});
</script>
<script src="cordova-2.3.0.js"></script>
<script src="jquery.mobile-1.2.0.min.js"></script>
</head>
<body>

<div data-role="page" id="page1">
    <div data-theme="a" data-role="header" data-position="fixed" >
        <h4>
            JLT
        </h4>

    </div>
    <div data-role="content" >

        <p>
        Send data to server?
        </p>
        <a data-role="button" id="transportYES" data-inline="true">
            Yes
        </a>
        <a data-role="button" id="transportNO" data-inline="true">
            No
        </a>


    </div>
</div>


<script>

$( "#transportYES" ).bind( "click", function(event, ui) {
  console.log('clicked');


    $.ajax({
   type: "GET",
   url: "http://api.wordnik.com//v4/word.json/recant/definitions",
   data: "includeRelated=false&includeTags=false&useCanonical=false&api_key=7741b711eec09c12e05070046d60da6b92e03750359859fde",
   dataType: "json",
   beforeSend:function(){
   console.log('before sending');
   },
   success: function(msg){
     alert( msg[0].word );
   },
   error: function(xhr, ajaxOptions, thrownError){
   console.log(xhr);

     alert( 'error' );
   }
 });


});

</script>
</body>
</html>

如果我從chrome運行此代碼,我會從服務器獲得響應,但是與phonegap打包時,它無法從android仿真器中運行。.我什至允許所有域如下配置: <access origin=".*"/> .xml文件..已在此問題上停留很長時間..請幫助.. !!!!

它可以在問題出在模擬器上的設備上工作.....無法更早地持有測試設備..本可以節省很多時間...... :)

您的mobileinit事件工作正常,除非您將此代碼放在jQuery和jQuery Mobile之間的文件中。

您必須執行以下操作(僅作為示例)

<script src="js/external/jquery-1.8.2.min.js"></script>
<script src="js/configs.js"></script>
<script src="js/external/jquery.mobile-1.2.0.js"></script>

在您的configs.js中,只需放置您的mobileinit

$( document ).bind( "mobileinit", function() {
   //your code
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM