Sunday, April 16, 2017

Laravel does not detect json with axios

I'm sending an ajax request with axios with these headers:

window.axios.defaults.headers.common = {
    'X-CSRF-TOKEN': window.Laravel.csrfToken,
    'X-Requested-With': 'XMLHttpRequest',
    'Content-Type': 'application/json'
};

But when I say this in a Laravel 5.4 controller:

if (request()->wantsJson()) {
    $forums = $this->forumInfo->index(Auth::user());
    return response()->json(compact('forums'), 200);
}

return view('home');

The json request is not detected. Also when I look into my headers in chrome dev I can see:

Content-Type:text/html; charset=UTF-8

Why does the header not change approximately to a json content type?



via Jenssen

Advertisement