2013-07-11 52 views
0

我正在做phonegap应用程序,其中我使用jquery ajax发送数据到服务器和作为响应我发送消息。当我在phonegap上运行它时,我总是给出错误信息。 我的代码ajax jquery回复提交表格

<!DOCTYPE HTML> 
<html> 
    <head> 
     <title>-</title> 

     <script type="text/javascript" charset="utf-8" src="cordova.js">  </script> 
<script type="text/javascript" src="jquery-2.0.3.min.js"></script> 



     <script> 
     function validation2() 
     { 
      alert('validation'); 
    alert("login"); 
    var server_url ="http://dealsscanner.com/labourapp/login_check.php"; 
    /* stop form from submitting normally */ 
    //event.preventDefault(); 

    /*clear result div*/ 

    /* get some values from elements on the page: */ 
    //var values = $(this).serialize(); 
    var values = { A1984 : 1, A9873 : 5, A1674 : 2, A8724 : 1, A3574 : 3, A1165 : 5 } 
    /* Send the data using post and put the results in a div */ 
    $.ajax({ 
     type: "GET", 
     url: server_url, 
     cache: false, 
     data: values, 
     success: function(msg){ 
     alert(msg); 

     }, 
     error:function(xhr, status, error) { 
     var err = eval("(" + xhr.responseText + ")"); 
    alert(err.Message); 

     } 
    }); 


     } 

$(function() { 


    $.ajax({ 
    url: "http://dealsscanner.com/labourapp/login_check.php", 
    dataType: 'jsonp', 
    jsonp: 'jsoncallback', 
    timeout: 15000, 
    success: function(data, status){ 
     //handle your data 
$('#response').html('There was no error loading the data.'); 
    }, 
    error: function(request, status, error) { 
     console.log("Error status " + status); 
     console.log("Error request status text: " + request.statusText); 
     console.log("Error request status: " + request.status); 
     console.log("Error request response text: " + request.responseText); 
     console.log("Error response header: " + request.getAllResponseHeaders()); 
     console.log("Error error: " + error); 
     $('#response').html('There was an error loading the data.'); 
     alert('There was an error loading the data'); 
    } 
}); 



}); 

     </script> 
    </head> 
    <body> 
    <div id ="response"></div> 
    <form id="form"> 
     <table> 
      <tr> 
       <th></th> 
       <th></th> 
      </tr> 
      <tr> 
       <td><h1> Login </h1></td> 
      </tr> 

      <tr> 
       <td> Enter email</td> 
       <td><input type="text" id ="email" name ="email" /> </td> 
      </tr> 
      <tr> 
       <td>Password</td> 
       <td><input type ="password" id ="pass" name ="pass"/></td> 
      </tr> 
      <tr> 
       <td colspan ="2"><input type="submit" id="submit" name ="submit" /></td> 
      </tr>  
     </table> 

    </body> 
    </form> 
</html> 

这里作为我的PHP文件只包含这个

<?php 
echo "ok"; 
?> 

回答

1

这很可能是由于跨站点脚本错误。如果你运行$.get("http://dealsscanner.com/labourapp/login_check.php"),那么你就可以看到错误:

XMLHttpRequest cannot load http://dealsscanner.com/labourapp/login_check.php?_=1373502259576 . Origin http://stackoverflow.com is not allowed by Access-Control-Allow-Origin.

有几种选择来解决这个问题:

  1. 修改您的服务器,以允许跨域Ajax调用。
  2. 修改您的服务器代码以处理JSONP请求,然后使用JQuery Ajax的“jsonp”数据类型。

第二个选择可能是最简单的 - 有一个简单的例子,说明如何在PHP中实现JSONP处理程序,here

+0

谢谢你,找出正确的问题,我检查这个链接,它解决了我的问题,但我没有得到从服务器的响应“确定”我提示它没有提供任何东西,但状态是成功http://支持.godaddy.com/groups/web-hosting/forum/topic/allow-cross-domain-request-using-jquery-ajax /我将xml复制并粘贴到网站的根目录 – Uahmed