Reputation: 57
I'm trying to pass a PDO associative array by way of JSON to some javascript code. I've verified that the php code works as far as producing a recordset, but I'm getting errors using jquery's $.parseJSON on the results. The first three values in the returned structure are all integers, and the parser understands them correctly. As soon as it gets to the first string value though, all bets are off. The reported error is an unexpected token, number, etc, depending on which non-numeric field is being parsed. Dates, strings, etc, all fall to the same problem.
I looked into other threads here on StackOverflow, and found much that seemed similar, but nothing that was quite the same problem.
Here's a sample of the returned JSON array (coming from php's json_encode, and snipped for brevity):
{ "idInventory":"451", "idAssociationGroup":"78","idMasterComponent":"601", "QuantityDescription":"Approx 4,450 GSY", "Quantity":null, "Location":"Hallways, Lobby, Locker Room, Library, and Lounge" }
The first three elements all post correctly via the code below, but QuantityDescription (and of course anything after it) fails.
And here's the part of part of the javascript code that's supposed to parse the results and place it on screen:
$.getJSON(
path + "/Inventory/getComponent.php",
{idComponent: idComponent},
function (data) {
document.getElementById('idInventory').innerHTML = $.parseJSON(data.idInventory);
document.getElementById('idAssociationGroup').innerHTML = $.parseJSON(data.idAssociationGroup);
document.getElementById('idComponentsUsed').innerHTML = $.parseJSON(data.idMasterComponent);
document.getElementById('QuantityDescription').innerHTML = $.parseJSON(data.QuantityDescription);
/* more fields go here, but I've snipped them for brevity, since *
* the error is already exposed */
}
);
Upvotes: 1
Views: 125
Reputation: 781004
Get rid of your calls to $.parseJSON
. The whole response is JSON, and $.getJSON
parses that for you automatically. The individual fields are just ordinary strings, they're not JSON.
So the first assignment should be:
document.getElementById('idInventory').innerHTML = data.idInventory;
and similar for all the other assignments.
Upvotes: 1