PHP JSON编码输出与Javascript Ajax不兼容


PHP JSON encoded output non compatible with Javascript Ajax

这里我构建了一个脚本来生成以下JSON输出:

[{"lat":41.081348,"lon":14.73292,"type":"F"},...,{"lat":41.09837,"lon":14.83176,"type":"F"}]

然而,当我尝试使用以下Javascript代码获取数据时:

        $.ajax({
            url: 'myJSONscript.php',
            dataType: "json",
            success: function (data, status, xhr) {
                console.log("data:"+data);
                console.log("status:"+status);
                console.log("xhr:"+xhr);
                }, //End Success
            error: function () {
                alert("ERROR");
                }
            });

我只得到一个空的数据变量
(换句话说,控制台返回
数据:为空
状态:成功,
xhr:[object object])
JSON输出是有效的(使用JSONlint测试),并且是使用以下代码构建的:

$arr=array();
while ($row=mysqli_fetch_array($r)) {
$element=array('lat'=>floatval($row['LATITUDE']),'lon'=>floatval($row['LONGITUDE']),'type'=>$row['TYPE']);
    array_push($arr, $element);
    }
$data=json_encode($arr,true);
header("Content-Type: application/json", true);
echo $data;

我搞不清楚出了什么问题。。。

已修复!contentType: "application/json"不能与dataType: "json"一起使用,删除contentType解决了问题。