小编Rap*_*DDL的帖子

json_encode创建格式错误的JSON(带有额外的隐藏字符)

我正在使用PHP json_encode()来返回一些由jQuery检索的数据ajax():

简化的JS:

$.ajax({
    dataType: 'json',
    contentType: 'application/json',
    cache: false,
    type: 'POST',
    url: './api/_imgdown.php',
    error: function(jqXHR, textStatus, errorThrow) {
        console.log(jqXHR, textStatus, errorThrow);
    },
    success: function(data, textStatus, jqXHR) {
        console.log(data, textStatus, jqXHR);
    }
});
Run Code Online (Sandbox Code Playgroud)

PHP是:

header('Content-Type: application/json; charset=UTF-8');
//default apiResponse
$apiResponse = [
    "status" => 1,
    "message" => "success",
    "data" => null
];
Run Code Online (Sandbox Code Playgroud)

然后当php运行我的代码时,它会通过添加这些数据结束:

$apiResponse['data'][] = [
    "mid" => (int)$mid,
    "card_type" => $card_type,
    "header_size" => (int)$headers['Content-Length'],
    "saved_size" => (int)filesize($imgSavePath),
    "saved_path" => $imgSavePath
];
//spit out the JSON
echo …
Run Code Online (Sandbox Code Playgroud)

php ajax jquery json parse-error

6
推荐指数
1
解决办法
8048
查看次数

标签 统计

ajax ×1

jquery ×1

json ×1

parse-error ×1

php ×1