I try to get the object from the controller and when I console.log(response)
it displays the values in it correctly
[ { "itemValue":100, "itemUnit":"2" } ]
Unfortunately, when my console says undefined
, I try to use an object like response.itemValue
. I try var object = response
. During console it shows the same value. I want to use response data.
if(itemID){ $.ajax({ type:'POST', url:'?syspath=ajax&controller=ajax&action=getActItemDose', data: { 'itemId': itemID, 'itemType': itemType }, success:function(response){ // var obj = jQuery.parseJSON(data); console.log(response); var object = response; var value = object.itemValue; var unit = object.itemUnit; console.log(object); console.log(value); } }); }
This is my controller encoding the object to Json
$row = $getProcess->fetch(); $object[] = array( 'itemValue' => $row['each_dose'], 'itemUnit' => $row['unit_dose'] ); echo json_encode($object);
By changing these few items, it worked
Enter
and JSON.parse(data)
I recommend using the jQuery library. To parse JSON, just do