Laravel 没有用 axios 检测到 json

Laravel does not detect json with axios

我正在发送 ajax 请求 axios 以及这些 headers:

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

但是当我在 Laravel 5.4 控制器中这样说时:

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

return view('home');

未检测到 json 请求。此外,当我在 chrome dev 中查看我的 headers 时,我可以看到:

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

为什么 header 不会大致更改为 json 内容类型?

也许如果您添加接受 header 并将其设置为 application/json 它可能会起作用。

示例:

'Accept': 'application/json'