来自ajax中的JSON响应的值返回为undefined

时间:2016-02-24 16:29:16

标签: 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);

    $('#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编码的响应,以及'响应'在控制台中打印(根据上面的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": [{}]
}

我正在尝试使用我经常使用的方法来检索客户详细信息,因此要获取firstname,我使用results.firstname其中results是一个已解析的JSON字符串,如我的jQuery响应中所写。

然而,我从results.firstname得到的只是undefined,即使明确定义了它。所以,基本上,我想知道我做错了什么?

希望有人可以解决我的问题。

2 个答案:

答案 0 :(得分:4)

您尝试访问的属性是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属性

答案 1 :(得分:0)

我能够像“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);