gpt4 book ai didi

javascript - laravel 无法使用 ajax 服务器状态 419 发布数据

转载 作者:行者123 更新时间:2023-12-02 22:45:53 27 4
gpt4 key购买 nike

我正在用 Laravel 做一个项目。

当我更改以下选择元素的选定选项时,我应该将选定的值插入到 mysql 数据库中,以从服务器检索有关选定 ID 的所有数据(例如用户名)。

这是选择元素(adminArea.blade.php):

<select name="employees" onchange="fillEmployeeData(this)" class="form-control col-sm-6" id="employees">
<option value="0"></option>
@foreach ($users as $user)
<option value="{{ $user->id }}">{{ $user->surname . ' ' . $user->name }}</option>
@endforeach
</select>

这是被调用的函数(adminArea.blade.php)::

function fillEmployeeData(emp_id) {
var emp_selected = emp_id.value;
$.ajax({
type: "POST",
url: "{{ route('adminAreaPostEmployee') }}",
data: 'emp_selected=' + emp_selected,
success: function (data) {
var emp_data = JSON.parse(data);
alert(emp_data);
}
});
};

这些是我的路线(web.php):

Route::get('/adminarea', 'AdminAreaController@index')->name('adminArea');
Route::post('/adminarea/postemployee', 'AdminAreaController@post_employee')->name('adminAreaPostEmployee');

这些是我的 Controller 方法(adminAreaController.php):

public function post_employee(Request $request)
{
$select = $request->get('emp_selected');
$user = User::where('id', $select);
echo $user;
}

public function index()
{
$operations = Operation::all();
$users = User::all()->sortBy('surname');
$rooms = Room::all();
return view('adminArea', compact('users', 'rooms', 'operations'));
}

但是,当我更改所选值时,没有任何反应...并且如果我转到开发人员工具,我会看到以下错误:

Failed to load resource: the server responded with a status of 419 (unknown status). Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException The GET method is not supported for this route. Supported methods: POST.

我没有看到任何警报。有人可以帮助我吗?

最佳答案

MethodNotAllowedHttpException 的 HTTP 状态代码为 405

参见here

public function __construct(array $allow, string $message = null, \Throwable $previous = null, ?int $code = 0, array $headers = [])
{
$headers['Allow'] = strtoupper(implode(', ', $allow));
parent::__construct(405, $message, $previous, $headers, $code);
}

TokenMismatchException HTTP 状态代码为 419

参见here

protected function prepareException(Exception $e)
{
if ($e instanceof ModelNotFoundException) {
$e = new NotFoundHttpException($e->getMessage(), $e);
} elseif ($e instanceof AuthorizationException) {
$e = new AccessDeniedHttpException($e->getMessage(), $e);
} elseif ($e instanceof TokenMismatchException) {
// HTTP Status code is send in the header here
$e = new HttpException(419, $e->getMessage(), $e);
} elseif ($e instanceof SuspiciousOperationException) {
$e = new NotFoundHttpException('Bad hostname provided.', $e);
}
return $e;
}

所以这似乎是 CSRF token 问题

确保文档的头部有一个元标记,如下所示

<meta name="csrf-token" content="{{ csrf_token() }}">

同样来自JQuery Ajax Documentation

我认为 HTTP 方法应该定义为 method 参数而不是 type (尽管 type 有效 ́\_(ツ)_/́)

// Send a CSRF token header with every Ajax request
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
function fillEmployeeData(emp_id) {
var emp_selected = emp_id.value;
$.ajax({
method: "POST",
url: "{{ route('adminAreaPostEmployee') }}",
data: 'emp_selected=' + emp_selected,
success: function (data) {
var emp_data = JSON.parse(data);
alert(emp_data);
}
});
};

但是现在你会得到一个错误

Object of class Illuminate\Database\Eloquent\Builder could not be converted to string

因为您尝试返回查询构建器实例 User::where('id', $select) 而不是序列化的用户记录本身

我想你可能想这样做

public function post_employee(Request $request)
{
$select = $request->get('emp_selected');
return User::find($select);
}

希望这有帮助

关于javascript - laravel 无法使用 ajax 服务器状态 419 发布数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58404002/

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