![](/img/trans.png)
[英]Returning JSON from an Ajax GET call - receiving an Undefined error on all but the first record in the JSON object
[英]undefined error on accessing json/object from laravel controller to ajax call
我试图在ajax jquery成功方法中操作从控制器返回的对象/ json。
我的代码如下所示-
在控制器上:
$product_code=$_GET['p_id'];
$productdata=Product::select('id','product_name','unit_measure','cost_price'
)->where('product_code','=',$product_code)->get();
return $productdata; //return object
return $productdata->toJson();//return json
在视图上(ajax调用):
success: function(data){
console.log(data);
console.log(data["product_name"]);
//for json [ console.log(data.product_name);
},
数据已成功接收,但是不知道为什么我在访问上述数据时收到未定义的错误。 提前致谢!
这正是我尝试使用json时得到的。 在控制台中输出:
[{"id":3,"product_name":"computer network","unit_measure":"piece","cost_price":500}]
undefined
由于响应对象位于数组中,因此未定义,这应该可以工作:
console.log(data.0.product_name);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.