2011-11-22 24 views
4

我想發送一些formdata到使用jQuery和Ajax的phonegap中的遠程服務器。我在下面粘貼的代碼在我的本地機器上的firefox中正常工作,但在我的手機(android)上啓動時,它不起作用。我是否需要修改或添加一些內容到jquery代碼中以使其在phonegap中工作?無法獲得jQuery發佈請求在手機上工作

我已經在android清單中添加了INTERNET權限。

<!DOCTYPE HTML> 
<html> 
    <head> 
     <title>Phonegap ajax test</title> 

     <meta name="viewport" content="width=240, height=320, user-scalable=yes, initial-scale=2.5, maximum-scale=5.0, minimum-scale=1.0" /> 
     <script type="text/javascript" charset="utf-8" src="phonegap-1.1.0.js"></script> 
     <script type="text/javascript" charset="utf-8" src="js/jquery.js"></script> 
     <script type="text/javascript" charset="utf-8" src="js/jquery.mobile-1.0rc2.js"></script> 

     <link rel="stylesheet" type="text/css" href="css/layout.css" /> 
     <link rel="stylesheet" type="text/css" href="js/jquery.mobile-1.0rc2.css" /> 

     <script type="text/javascript">  
      $(document).ready(function() { 
       $('#infoForm').submit(function() { 
        var postTo = 'http://www.mysite.com/process.php'; 

        $.post(postTo,$('#infoForm').serialize(), function(data) { 
         alert(data);  
        }); 
        return false; 
       }); 

      }); 
     </script> 
    </head> 

    <body>  
     <div data-role="content"> 
      <form method="post" id="infoForm"> 
       <input type="text" name="first_name" id="first_name" value="" placeholder="First Name" /> 
       <input type="text" name="last_name" id="last_name" value="" placeholder="Last Name" /> 
       <input type="text" name="email" id="email" value="" placeholder="Email" /> 
       <button type="submit">Submit</button> 
      </form> 
     </div><!-- /content --> 
    </body> 
</html> 

謝謝!

回答

0

以下代碼可以在Android設備/模擬器中使用,您可以使用jquery-1.4.2.js的ajax功能。

<html> 
<head> 
<script src="js/jquery-1.4.2.js" type="text/javascript" charset="utf-8"></script> 
<script src="js/jquery-1.4.2.min.js" type="text/javascript"></script> 
<script> 
function bodyload(){ 
    alert("We are calling jquery's ajax function with the post type request"); 
$.ajax({ 
    url:'http://www.edumobile.org/blog/uploads/XML-parsing-data/Data.xml', 
    dataType:'application/xml', 
    timeout:10000, 
    type:'POST', 
    success:function(data) { 
     alert(data); 
    }, 
    error:function(XMLHttpRequest,textStatus, errorThrown) {  
     alert("Error status :"+textStatus); 
     alert("Error type :"+errorThrown); 
     alert("Error message :"+XMLHttpRequest.responseXML); 
    } 
    }); 
} 
</script> 
</head> 
<body onload="bodyload()"> 
<button onclick="bodyload()">Ajax call</button> 
</body> 
</html>
+0

謝謝!我會試試這個! –

5

確保您的應用擁有正確的權限。

添加

<uses-permission android:name="android.permission.INTERNET" /> 

AndroidManifest.xml中並添加

<access origin="*"/> 

/res/xml/phonegap.xml

相關問題