Laravel JSON响应缺失了右方方括号,Ajax抛出了Parseerror
查看
$.ajax({
type: 'GET',
url: '/company/ajaxGetClients/1',
dataType: 'json',
success: function(response) {
console.log(response); // Expecting this response logged in console.
},
error: function (a, e){
console.log(e); // This logs "parseerror".
}
})
Company Controller
public function ajaxGetClients($id)
{
return response(Company::findOrFail($id)->clients->toArray()); // versions without toArray() or with toJson() also does not work. response()->json() same result.
}
响应,
[{"id":1,"name":"John","company_id":1,"phone":null,"email":null,"created_at":null,"updated_at":null}
如您所见,响应缺少结束括号]
使JSON无效和Ajax无法解析它。这是Laravel 9.15中的错误还是我做错了什么?
View
$.ajax({
type: 'GET',
url: '/company/ajaxGetClients/1',
dataType: 'json',
success: function(response) {
console.log(response); // Expecting this response logged in console.
},
error: function (a, e){
console.log(e); // This logs "parseerror".
}
})
CompanyController
public function ajaxGetClients($id)
{
return response(Company::findOrFail($id)->clients->toArray()); // versions without toArray() or with toJson() also does not work. response()->json() same result.
}
Response
[{"id":1,"name":"John","company_id":1,"phone":null,"email":null,"created_at":null,"updated_at":null}
As you can see, response is missing ending bracket ]
making json invalid and ajax fails to parse it. Is it a bug in Laravel 9.15 or am I doing something wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
他们是错误的语法。尝试以下操作: -
They are wrong syntax.. Try this:-