簡體   English   中英

Ajax發布請求將發送的數據附加到返回的JSON

[英]Ajax post request is appending sent data to the returned JSON

我有一個像這樣的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);
    }
});

哪個被發送到並處理:

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
)

因此,看來我傳遞給調用的數據被追加到數據的末尾。 如何防止這種情況發生?

看起來您可能在Array變量的某處進行了print_r嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM