2015-02-10 64 views
0

我一直在尋找一個解決方案,我得到一個我正在建設的phonegap應用程序的錯誤。Phonegap Ajax Post Error

HTML

<form method="post" id="app-send" name="app-send"> 
     <input id="a" name="a" type="text"> 
     <input id="b" name="b" type="text"> 
     <input id="c" name="c" type="text"> 
     <input id="timestamp" name="timestamp" type="text"> 
     <input id="submit" name="submit" type="submit"> 
    </form> 

JS

<script type="text/javascript"> 
     $(document).ready(function() { 
      $('#app-send').submit(function (e) { 
       var postData = $(this).serialize(); 
       $.ajax({ 
        type: 'POST', 
        url: 'http://example.com', 
        dataType: 'json', 
        data: postData, 
        success: function (data) { 
         alert(data); 
        }, 
        error: function() { 
         alert('error!'); 
        } 
       }); 

       return false; 
      }); 
     }); 
    </script> 

我的問題:

每次我在控制檯上的PhoneGap開發的應用程序測試我回來

"Proxy error for url: http://example.com undefined" 
"HPE_INVALID_CONTACT http://example.com" 

UPDATE

我剛纔試過這樣:

<form action="http://www.example.com" method="post" id="app-send" name="app-send"> 
    <input id="a" name="a" type="text"> 
    <input id="b" name="b" type="text"> 
    <input id="c" name="c" type="text"> 
    <input id="timestamp" name="timestamp" type="text"> 
    <input id="submit" name="submit" type="submit"> 
</form> 

而這個工作。那麼我怎樣才能使用AJAX提交數據並停止改變頁面呢?

回答

0

我相信你正在使用jQuery Mobile的

的PhoneGap的禁令,您應該添加data-ajax="false"你的表單元素停止jQuery Mobile的默認行爲。

0

請嘗試添加e.preventDefault()停止默認行爲

$(document).ready(function() { 
     $('#app-send').submit(function (e) { 
       e.preventDefault(); 
      //rest of your code 
     }); 
    });