我正在制作phonegap应用程序,其中我使用jquery ajax将数据发送到服务器并作为响应我正在发送消息。当我在phonegap上运行它时,我总是给出错误消息。 我的代码
<!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";
?>
答案 0 :(得分:1)
这很可能是由于跨站点脚本错误造成的。如果您运行$.get("http://dealsscanner.com/labourapp/login_check.php")
,则可以看到错误:
XMLHttpRequest无法加载http://dealsscanner.com/labourapp/login_check.php?_=1373502259576。 Access-Control-Allow-Origin不允许原点http://stackoverflow.com。
有几种方法可以解决这个问题:
第二个选项可能是最简单的 - 有一个如何在PHP中实现JSONP处理程序的简单示例,here。