Ajax post请求将发送的数据附加到返回的JSON中

Ajax post request is appending sent data to the returned JSON

本文关键字:返回 JSON 数据 请求 Ajax post      更新时间:2023-09-26

我有一个ajax调用像这样:

$.ajax({
    url: '/assets/functions.php',
    type: 'POST',
    data: {
        "functionCall": "get-uploads",
        "type": type
    },
    dataType: 'json',
    success: function (data, textStatus) {
        console.log("done");
        console.log(data);
        console.log(textStatus);
    },
    error: function(textStatus, errorThrown) {
        console.log("uh oh");
        console.log(textStatus);
        console.log(errorThrown);
    }
});

它被发送到并用this处理:

switch($_POST['functionCall']) {
    .
    .
    .
    case "get-uploads":
        $type = $_POST['type'];
        $getUploads = "SELECT * FROM pp_uploads WHERE type = '$type';";
        $docArray = array();
        while($row = mysql_fetch_assoc($documents)) {
            $docArray[] = $row;
        }
        echo json_encode($docsArray);
}

当我运行这个时,我得到一个解析错误,这从我的理解意味着返回的数据没有作为JSON返回。因此,我将dataType更改为html,并且我看到控制台中返回的数据是:

[{"id":"35","filename":"fdgsdf","path":"ConfiguratorTreeDiagram.pdf","type":"resources"},{"id":"36","filename":"gsrewg","path":"dhx_advertising.pdf","type":"resources"}]Array
(
    [functionCall] => get-uploads
    [type] => resources
)

所以看起来我传递给调用的数据被附加到我的数据的末尾。我该如何防止这种情况发生呢?

看起来你可能在数组变量的某个地方做print_r ?