ajax jquery response in submit form -
i making phonegap application in use jquery ajax send data server , in response sending message . when run on phonegap give error message . 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 submitting */ //event.preventdefault(); /*clear result div*/ /* values elements on page: */ //var values = $(this).serialize(); var values = { a1984 : 1, a9873 : 5, a1674 : 2, a8724 : 1, a3574 : 3, a1165 : 5 } /* send data using post , put results in 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 data $('#response').html('there no error loading 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 error loading data.'); alert('there error loading 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 php file contain this
<?php echo "ok"; ?>
this due cross-site scripting error. if run $.get("http://dealsscanner.com/labourapp/login_check.php")
, can see error:
xmlhttprequest cannot load http://dealsscanner.com/labourapp/login_check.php?_=1373502259576. origin http://stackoverflow.com not allowed access-control-allow-origin.
there several options around this:
- modify server allow cross-domain ajax calls.
- modify server code handle jsonp requests, make use of jquery ajax's "jsonp" data type.
the second option easiest -- there's simple example of how implement jsonp handler in php, here.
Comments
Post a Comment