I have three data coming back from an ajax call. I pack them into an array like this:
return json_encode([$salesOrder, $soAddressDetails, $lineItems]);
I then go to the view and look at the return. I see (as an example) this:
[
[{
"id": 8591,
"reference": "MYCLIENT",
"name": "MYCLIENT COMPANY \u00a3",
"allocated_status": "",
"created_at": "2016-12-02 09:31:00",
"order_date": "2016-12-02",
"cust_order_number": "",
"del_name": "",
"consignment": "",
"despatch_date": "0000-00-00",
"notes_2": ""
}],
[],
[{
"id": 11691,
"qty_ordered": 1,
"qty_delivered": 0,
"sales_order_id": 8591,
"due_date": "2016-12-30",
"stock_code": "ABC-ABDCDE-01",
"record_deleted": 0,
"updated_at": null,
"unit_price": 0,
"sales_order_item_id": null,
"comment": null,
"created_at": null,
"firmware_version": null,
"units_assigned": null
},
{
"id": 11692,
"qty_ordered": 1,
"qty_delivered": 0,
"sales_order_id": 8591,
"due_date": "0000-00-00",
"stock_code": "MISCELLANEOUS",
"record_deleted": 0,
"updated_at": null,
"unit_price": 232,
"sales_order_item_id": null,
"comment": null,
"created_at": null,
"firmware_version": null,
"units_assigned": null
}
]
]
In theory all I should need to access this, as array result
is:
result[0] // sales order details
result[2] // line items = array of objects
so
result[0].reference == 'MYCLIENT'
and
result[2][0].stockcode == 'ABC-ABDCDE-01
but it won't let me do that. if I console.log(result[0])
the result is [
, if I console.log(result[0][0].id)
the result is undefined
.
what am I doing wrong?
Either you need to say that your response will be Json in your ajax like this
dataType: 'json'
Or after getting response you have to convert it into json object
response = JSON.parse(response);
From the last line of your question, seems like your result
is still a string.
Try doing JSON.parse(result)
I think you just miss the level. The reference it's also in a second level. So you need to access it like:
console.info(result[0][0].reference)
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.