$.ajax response is null?


$.ajax response is null?

在我的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上检查我的firebug时,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请求。