[英]Laravel returning empty json object with ajax request
我有一個ajax請求,誰正在調用php函數,誰正在對一些結果進行排序並返回對象數組。
返回包含我想要的結果,但是當我使用Response :: json($ result)將其返回時,該數組返回的很好,但是具有空數組(在javascript中)。
public static function getResult($category = null, $artist = null, $search = null, $data)
{
$result = [];
if ($category != null)
{
if ($category < 20)
{
foreach ($data as $key => $value)
{
if (is_array($value->getCategories()))
{
foreach ($value->getCategories() as $key1 => $value1)
{
if ($value1 == $category)
{
array_push($result, $value);
}
}
}else {
if ($value->getCategories() == $category)
{
array_push($result, $value);
}
}
}
}
}
if ($artist != null)
{
foreach ($data as $key => $value)
{
if (stripos(strtolower($value->getArtist()), strtolower($artist)) !== false)
{
array_push($result, $value);
}
}
}
if ($search != null)
{
foreach ($data as $key => $value)
{
if (stripos(strtolower($value->getArtist()), strtolower($search)) !== false || stripos(strtolower($value->getTitle()), strtolower($search)) !== false)
{
array_push($result, $value);
}
}
}
return $result;
}
路線
Route::get('/browse/categories/{id}', function($id){
$result = App\Http\Controllers\browseController::getResult($id, null, null, session("TotalShop"));
// return json_encode(count($result));
return response()->json($result);
});
Java腳本
$(".categories").click(function(event){
$.get("/browse/categories/" + event.target.id, function(data){
console.log(data);
});
});
那么,我該怎么做才能在javascript部分中獲取我的php對象的內容?
經過研究后,我需要將php數組而不是Php對象傳遞給我的Json響應。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.