[英]AJAX JSON response is returning the correct values, but also undefined values in between?
[英]Values from a JSON response in ajax returning as undefined
嘗試從通過jQuery中的$.post()
方法發送的JSON響應中檢索值時遇到一些問題。 這是腳本:
var clickedName = $('#customerId').val();
$.post("/customer-search", { name: clickedName }).done( function(response) {
var results = $.parseJSON(response);
console.log(results);
$('#account-name').html(results.firstname + ' ' + results.lastname);
$('#email').html(results.email);
$('#telephone').html(results.telephone);
if (results.fax) {
$('#fax').html(results.fax);
} else {
$('#fax').html('n/a');
}
$('#personal').fadeIn();
return false;
});
只是為了說明一下,我在Symfony2項目中使用了Twitter typeahead,基本上,當鍵入后從列表中單擊(選擇)名稱時,此腳本將觸發。 客戶搜索URL如下運行數據庫搜索:
$q = $request->request->get('name');
$em = $this->getDoctrine()->getManager();
$customer = $em->getRepository('AppBundle:Oc73Customer')->findLikeName($q);
$addresses = $em->getRepository('AppBundle:Oc73Address')->findByCustomerId($customer[0]['customerId']);
$results = array();
$results['customer'] = $customer;
$results['addresses'] = $addresses;
return new Response(json_encode($results));
它將成功返回Json編碼的響應,並且在控制台中打印的“ response”值(根據上述jquery)為:
{
"customer": [{
"firstname": "Mike",
"lastname": "Emerson",
"email": "xxxx@xxxx.co.uk",
"telephone": "01234 5678910",
"fax": null,
"password": "8e1f951c310af4c20e2cd6b68dee506ac685d7ae",
"salt": "e2b9e6ced",
"cart": null,
"wishlist": null,
"newsletter": 0,
"addressId": 84,
"customerGroupId": 1,
"ip": null,
"status": 1,
"approved": 1,
"token": null,
"dateAdded": {
"date": "2016-02-16 12:59:28.000000",
"timezone_type": 3,
"timezone": "Europe/Berlin"
},
"availCredit": null,
"customerId": 75
}],
"addresses": [{}]
}
我試圖通過使用我經常使用的方法來檢索客戶詳細信息,因此要獲取名字,我使用results.firstname
,其中results是解析的JSON字符串,如我的jQuery響應中所述。
但是,我從results.firstname
獲得的所有內容都是undefined
,即使明確定義了它也是如此。 所以,基本上,我想知道我做錯了什么?
希望有人可以闡明我的問題。
您嘗試訪問的屬性是customer
數組中的對象,而不是父對象本身。 假設響應僅包含一個客戶對象,則可以使用result.customer[0]
,如下所示:
$.post("/customer-search", { name: clickedName }).done(function(response) {
var results = $.parseJSON(response);
var customer = response.customer[0];
$('#account-name').html(customer.firstname + ' ' + customer.lastname);
$('#email').html(customer.email);
$('#telephone').html(customer.telephone);
$('#fax').html(customer.fax ? customer.fax : 'n/a');
$('#personal').fadeIn();
});
如果有可能在數組中返回多個customer
對象,則需要修改代碼以遍歷這些對象並構建HTML以顯示所有對象,而無需使用id
屬性。
我可以像“ results.customer [0] .firstname”一樣訪問它
var cus =
{
"customer": [{
"firstname": "Mike",
"lastname": "Emerson",
"email": "xxxx@xxxx.co.uk",
"telephone": "01234 5678910",
"fax": null,
"password": "8e1f951c310af4c20e2cd6b68dee506ac685d7ae",
"salt": "e2b9e6ced",
"cart": null,
"wishlist": null,
"newsletter": 0,
"addressId": 84,
"customerGroupId": 1,
"ip": null,
"status": 1,
"approved": 1,
"token": null,
"dateAdded": {
"date": "2016-02-16 12:59:28.000000",
"timezone_type": 3,
"timezone": "Europe/Berlin"
},
"availCredit": null,
"customerId": 75
}],
"addresses": [{}]
}
alert(cus.customer[0].firstname);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.