Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

maogu's avatar
Level 1

I hope that the front end can receive the data returned by the controller after the post, how can I do it?

const submit = () => {
    router.post(route('searches.store'),{'keyword':keyword.value,'type':tab.value},).then((res) => {
        console.log(res.data)
    })
};
   public function store(Request $request)
    {
        if ($request->input('type', 'comic') == 'comic') {
            $data = Comic::query()->where('name', 'like', '%'.$request->input('keyword').'%')->with(['reader:id,name'])->take(30)->get();

        }

        return  redirect()->back()->with('data', $data);
    }
0 likes
3 replies
Ben Taylor's avatar

First of all your $data variable is defined inside an if statement. If the statement isn't true, then you'll have an issue.

Second, I assume you are wanting a json response rather than a redirect. Try

return response()->json([
 'data' => $data
], 200);
maogu's avatar
Level 1

@Ben Taylor

`` All Inertia requests must receive a valid Inertia response, however a plain JSON response was received. {"data":"111"}

Please or to participate in this conversation.