简体   繁体   中英

Ajax post request is appending sent data to the returned JSON

I have an ajax call like so:

$.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);
    }
});

Which gets sent to and handled with 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);
}

When I run this I get a parsing error, which from what I understand means that the returned data isn't being returned as JSON. So I changed the dataType to html, and I see that the returned data in the console is:

[{"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
)

So it looks like the data I passed into the call is being appended to the end of my data. How do I prevent that from happening?

看起来您可能在Array变量的某处进行了print_r吗?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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