Javascript ajax中JSON响应返回的未定义值
我在尝试从通过jQuery中的Javascript ajax中JSON响应返回的未定义值,javascript,php,jquery,json,Javascript,Php,Jquery,Json,我在尝试从通过jQuery中的$.post()方法发送的JSON响应中检索值时遇到了一些问题。以下是脚本: var clickedName = $('#customerId').val(); $.post("/customer-search", { name: clickedName }).done( function(response) { var results = $.parseJSON(response); console.log(results);
$.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)为:
我试图使用我一直使用的方法检索客户详细信息,因此为了获得firstname,我使用results.firstname
,其中results是一个解析的JSON字符串,如jQuery响应中所述
然而,我从结果中得到的一切。firstname
是未定义的
,即使它已明确定义。所以,基本上,我想知道我做错了什么
希望有人能解释一下我的问题。您试图访问的属性是
customer
数组中的对象,而不是父对象本身。假设响应只包含一个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
属性。您尝试访问的属性是customer
数组中的对象,不在父对象本身上。假设响应只包含一个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”一样访问它
我可以像“results.customer[0].firstname”一样访问它
因为没有
结果。firstname
属性-有一个customer
属性-它包含一个对象数组-它有一个firstname
属性:results.customer[0]。firstname
应该可以工作。因为结果是一个对象数组,所以您应该循环客户(通过循环结果)或者像这样访问它们:results.customer[0].firstname代码>。这里的例子:(很好的尝试,问题也很明显)如果您还没有使用JSON解析工具(比如,我建议将一个添加到您的存储库中。因为没有结果。firstname
属性-有一个customer
属性-它包含一个对象数组-它有一个firstname
属性:results.customer[0]。firstname
应该可以工作。因为结果是一个对象数组,您应该循环客户(通过循环结果),或者像这样访问他们:results.customer[0]。firstname代码>。这里的例子:(很好的尝试,而且问题暴露得很好)如果您还没有使用JSON解析工具(比如),我建议您在您的武库中添加一个。
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);