0

i am making phonegap application in which i use jquery ajax to send data to server and in response i am sending message . When i run it on phonegap i always give the error message . my code

   <!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>

where as my php file only contain this

<?php
 echo "ok";
?>
Uahmed
  • 1,847
  • 5
  • 29
  • 45

1 Answers1

1

This is most likely due to a cross-site scripting error. If you run $.get("http://dealsscanner.com/labourapp/login_check.php"), then you can see the error:

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

There are several options to get around this:

  1. Modify your server to allow cross-domain Ajax calls.
  2. Modify your server code to handle JSONP requests, then make use of JQuery Ajax's "jsonp" data type.

The second option is probably easiest -- there's a simple example of how to implement a JSONP handler in PHP, here.

Community
  • 1
  • 1
McGarnagle
  • 101,349
  • 31
  • 229
  • 260
  • Thank you , you figure it out the right problem , i check this link it solve my issue but i dont get the response "ok" from the server i putt alert it gives nothing but the status is sucess http://support.godaddy.com/groups/web-hosting/forum/topic/allow-cross-domain-request-using-jquery-ajax/ I copy the xml and paste it to the root of the site – Uahmed Jul 11 '13 at 00:56