2013-02-13 51 views
2

我想創建與導航索引點擊將加載通過Ajax而不是去到另一個HTMLPhoneGap的導航阿賈克斯

頁面時,這是我的javascipt的:

<script type="text/javascript" src="js/jquery.js"></script> 
<script type="text/javascript"> 
    function getPage(){ 
      $.ajax({ 
       type: 'post', 
       url: 'places.html', 
       success: function(html) 
       { 
        $("#response").fadeIn("slow"); 
        $("#response").html(html);   

       } 
      });  

      return false;  
     } 
    </script> 

我有一個調用這個

<a href="" onclick="getPage();">get page</a> 

這個工作了XAMPP本地主機,但似乎在PhoneGap的生成錯誤的href

Uncaught ReferenceError: $ is not defined at file:///android_asset/www/index.html:129 

這哪裏是$就位於

+0

的phonegap項目中文件的路徑是否正確? – dan 2013-02-13 08:41:35

回答

1

你必須等待,直到設備準備好進行Ajax調用就行了。

document.addEventListener("deviceready", onDeviceReady, false); 
function onDeviceReady() { 
function getPage(){ 
     $.ajax({ 
      type: 'post', 
      url: 'places.html', 
      success: function(html) 
      { 
       $("#response").fadeIn("slow"); 
       $("#response").html(html);   

      } 
     });  

     return false;  
    } 
} 
+0

好了解決了$未定義的問題,但我得到了02-14 09:56:32.180:E/Web控制檯(20736):未捕獲的ReferenceError:getPage未在file:/// android_asset/www/index處定義。 html:127,爲什麼會發生這種情況? – jhdj 2013-02-14 01:58:00

1

以及阿賈克斯的工作去年所需要的完整的文件地址,並把它裏面documentready

$.ajax({ 
     type: 'post', 
     url: 'file:///android_asset/www/.html/places.html', 
     success: function(html) 
     { 
      $("#response").fadeIn("slow"); 
      $("#response").html(html);   

     } 
    });  

另一種方法,我發現是

$("#getProfile").click(function() { 
      var ajax = new XMLHttpRequest(); 
      ajax.open("GET","file:///android_asset/www/places.html",true); 
      ajax.send(); 

      ajax.onreadystatechange=function(){ 
       if(ajax.readyState==4 && (ajax.status==200||ajax.status==0)){    
        var theHTML = ajax.responseText; 
        document.getElementById('response').innerHTML = theHTML; 
       } 
      } 

     }); 

感謝您的幫助馬利克