gpt4 book ai didi

json - Laravel $ request-> expectsJson()

转载 作者:行者123 更新时间:2023-12-03 09:47:47 25 4
gpt4 key购买 nike

我正在为我的Laravel应用程序进行Ajax登录。
我正在使用Angular:

$http({
method: 'POST',
url: '/admin/login',
headers: {
'Content-Type': 'application/json'
},
data: {email:$scope.email,password:$scope.password}
})
这个请求工作正常,我的问题是Laravel的响应总是重定向我,如果我不发送JSON,它应该重定向:
protected function sendFailedLoginResponse(Request $request)
{
$errors = [$this->username() => trans('auth.failed')];
if ($request->expectsJson()) {
return response()->json($errors, 422);
}
return redirect()->back()
->withInput($request->only($this->username(), 'remember'))
->withErrors($errors);
}
那就是Laravel框架代码。它一直将我重定向回去,但是我想触发条件 if ($request->expectsJson())并获取JSON响应而不是重定向。
为了触发该条件,我缺少什么?
我什至补充说:
 headers: {
'Content-Type': 'application/json','X-Requested-With' :'XMLHttpRequest'
}
而且它仍然行不通。
expectsJson():
public function expectsJson()
{
return ($this->ajax() && ! $this->pjax()) || $this->wantsJson();
}
我的标题:

Accept:application/json, text/plain, /

Accept-Encoding:gzip, deflate

Accept-Language:en-US,en;q=0.8

Connection:keep-alive

Content-Length:57

Content-Type:application/json

X-Requested-With:XMLHttpRequest


出于安全原因,我不包括 token /cookie和原始URL,但它们都在其中。
编辑:我试图清除工匠缓存,仍然一无所获。 php artisan cache:clearcomposer dump-autoload

最佳答案

您应该使用Accept key 而不是Content/type。有关更多详细信息,请检查this github discussion

enter image description here

关于json - Laravel $ request-> expectsJson(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45015586/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com