来自 ajax 中 JSON 响应的值返回为未定义

Values from a JSON response in ajax returning as undefined

本文关键字:返回 未定义 响应 ajax JSON 来自      更新时间:2023-09-26

尝试从通过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": [{}]
}

正在尝试使用我一直使用的方法检索客户详细信息,因此要获取名字,我使用 results.firstname 其中结果是解析的 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);