我可以使用laravel从我的数据库中提取数据并将其发送回kendo自动完成。
我遇到的问题是我收到错误:
未捕获的TypeError:undefined不是函数
我有一种感觉,因为返回的数据格式错误,即:
{"16159":"United Kingdom of Great Britain and Northern Ireland"}
什么时候应该像
那样[{"id": 16159, "name":"United Kingdom of Great Britain and Northern Ireland"}, *other counties...*]
我的Laravel代码是:
public function getCities() {
$Cities = new Cities;
$cities = $Cities->where('accentName', 'LIKE', '%unite%')->orderBy('accentName', 'asc')->lists('accentName', 'id');
return $cities;
}
我的JS是:
$('input#cityAuto').kendoAutoComplete({
dataTextField: "name",
filter: "contains",
minLength: 3,
dataSource: {
type: "POST",
serverFiltering: true,
transport: {
read: "apply/getCities"
}
}
});
如何让Laravel以我想要的方式返回JSON字符串?
答案 0 :(得分:0)
您可以使用Response::json()
执行此操作,如下所示:
public function getCities() {
$Cities = new Cities;
$cities = $Cities->where('accentName', 'LIKE', '%unite%')->orderBy('accentName', 'asc')->lists('accentName', 'id');
// to return json
return Response::json($cities->toArray());
}