ConorBaumgart ConorBaumgart - 29 days ago 23
PHP Question

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?

Answer

It looks like you might be doing a print_r somewhere on an Array variable?