简体   繁体   English

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

[英]PHP JSON encoded output non compatible with Javascript Ajax

Here I've built a script to generate the following JSON output: 在这里,我构建了一个脚本来生成以下JSON输出:

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

However when I try to get data using the following Javascript code: 但是,当我尝试使用以下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");
                }
            });

I get just an empty data variable 我只得到一个空的数据变量
(in other words the console returns (换句话说,控制台返回
data: empty , 数据:
status:success, 状态:成功,
xhr:[object Object]). xhr:[object Object])。
The JSON output is valid (tested with JSONlint ) and was built using the following code: 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;

I cannot figure out what's wrong... 我不知道怎么了...

Fixed! 固定! the contentType: "application/json" cannot be used together with dataType: "json" , removing contentType solved the problem. contentType: "application/json"不能与dataType: "json"一起使用,删除contentType解决了该问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM