【问题标题】:unable to fetch the JSON Data using jquery in ajax [duplicate]无法在ajax中使用jquery获取JSON数据[重复]
【发布时间】:2018-07-30 13:36:00
【问题描述】:

我无法在 ajax 中使用 jquery 获取 JSON 数据。Getting Uncaught TypeError: Cannot read property 'customers' of undefined 错误。

<script type="text/javascript">

$("#savechanges").click(function(e) {
e.preventDefault();


jQuery.ajax({
url: "<?=base_url()?>customers/updatecustomerorderdetail",
data: $('#savecustomer input').serialize(),
type: "POST",
dataType: 'json',
beforeSend: function() {
        //$("#update_"+id).html('');
        $("#savechanges").html('<i class="fa fa-spinner fa-spin"></i>updating...');

       },
success:function(data){

        var customer_name = data[0].customers[0].customer_name;

        alert(customer_name);

        console.log(data);


 },
error:function (error){

    console.log(error);
}

});

});

来自上述代码的 JSON 响应

{  
 "customers":[  
   {  
     "customer_id":22,
     "customer_name":"fggfd",
     "customer_email":"fggd",
     "customer_mobile":"dfgf",
     "updated_user_id":"5",
     "updated_datetime":"2018-07-30 21:00:57"
  }
]
}

我想从 ajax 成功函数中的 JSON 数据中提醒customer_name。谁能告诉我我在这里做错了什么?

【问题讨论】:

    标签: javascript jquery ajax


    【解决方案1】:

    您的数据显然是一个对象而不是数组,所以这样做: data.customers[0].customer_name;

    【讨论】:

      猜你喜欢
      • 2018-05-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-01
      • 2018-12-02
      • 2018-04-09
      相关资源
      最近更新 更多