$ .ajax响应为空?
问题描述:
在我的javascript我有,
$.ajax({
type: 'GET',
url: 'http://133.333.33.33/reporting/summary_table.php?loc_id='+locid+'&loc_type='+loctype+'',
async: false,
success: function(data) {
alert(data);
},
dataType: 'json'
});
在我的服务器端我有这个,
$result = mysql_query($query);
$rows = array();
while ($row = mysql_fetch_assoc($result)) {
$rows[] = $row;
}
echo json_encode($rows);
当我检查我的FF萤火虫,Ajax响应是什么,它激发出一个错误,而不是。我错过了什么?
答
打电话给你的服务如下:
$.ajax({
url: 'http://yourserver.com/reporting/summary_table.php?loc_id='+locid+'&loc_type='+loctype+'',
success: function(data) {
alert(data);
},
dataType: 'jsonp'
});
当心“JSONP”数据类型。
然后实现在你的服务器端脚本这个小变化:
$jsonp = json_encode($rows);
if(isset($_GET["callback"])) $jsonp = $_GET["callback"]."($jsonp)";
echo $jsonp;
所以你的PHP这会是能够回答JSONP请求。
除非服务器支持并启用CORS,否则无法进行跨域Ajax调用。或者你必须使用JSONP,它也必须得到服务器的支持。 – 2013-02-21 17:28:49
“它会发出错误,而不是” - **什么**错误? – Quentin 2013-02-21 17:29:33
您正在使用[an **过时的**数据库API](http://*.com/q/12859942/19068),并应使用[现代替换](http://php.net/manual/en/ mysqlinfo.api.choosing.php)。 – Quentin 2013-02-21 17:30:34