我在通过Laravel AJAX请求传递参数时遇到麻烦。有很多类似的问题,但似乎都没有解决我遇到的问题。
这是我的Laravel路线:
Route::get('workerAjax', function(){
return View::make('workerAjax');
});
Route::post('/workerAjax/post', 'WorkerController@storeWorker');
WorkerController.php
<?php
class WorkerController extends \BaseController {
public function storeWorker(Request $request){
return 'I am in';
}
}
workerAjax.blade.php
<!doctype html>
<html lang="{{ app()->getLocale() }}">
<head>
<meta charset="utf-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<meta name="_token" content="{{ csrf_token() }}" />
<title>Register worker</title>
</head>
<body>
<div class="container">
<div class="alert alert-success" style="display:none"></div>
<form id="myForm">
<!-- don't think I need this, but it doesn't work with or without -->
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<div class="form-group">
<label for="firstName">First Name:</label>
<input type="text" id="firstName">
</div>
<div class="form-group">
<label for="lastName">Last Name:</label>
<input type="text" id="lastName">
</div>
<div class="form-group">
<label for="email">Email:</label>
<input type="text" id="email">
</div>
<div class="form-group">
<label for="password">Password:</label>
<input type="password" id="password">
</div>
<button class="btn btn-primary" id='ajaxSubmit'>Submit</button>
</form>
</div>
<script src="https://code.jquery.com/jquery-3.3.1.min.js"
integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
crossorigin="anonymous">
</script>
<script>
jQuery(document).ready(function(){
jQuery('#ajaxSubmit').click(function(e){
e.preventDefault();
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
}
});
jQuery.ajax({
url: "{{ url('/workerAjax/post') }}",
method: 'post',
data: {
firstName: jQuery('#firstName').val(),
lastName: jQuery('#lastName').val(),
email: jQuery('#email').val(),
password: jQuery('#password').val(),
},
success: function(result){
jQuery('.alert').show();
jQuery('.alert').html(result.success);
}});
});
});
</script>
</body>
</html>
</html>
在浏览器中填写表格并单击提交后,控制台中出现POST错误500。 网络错误的预览显示此消息
error: {type: "Symfony\Component\Debug\Exception\FatalThrowableError",…}
file: "/path/to/folder/app/controllers/WorkerController.php"
line: 64 ( public function storeWorker(Request $request){ )
message: "Type error: Too few arguments to function WorkerController::storeWorker(), 0 passed and exactly 1 expected"
type: "Symfony\Component\Debug\Exception\FatalThrowableError"
我觉得我做对了所有事情,但仍然无法正常工作。可以给有经验的框架的人帮忙吗?
编辑: 这是通过请求发送的数据: XHR Request
修改2: 我的文件夹结构可能是一个难题,这是: App folder structure
答案 0 :(得分:0)
将 ajax 对象中的method
键更改为type
。
<script>
jQuery(document).ready(function(){
jQuery('#ajaxSubmit').click(function(e){
e.preventDefault();
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
}
});
jQuery.ajax({
url: "{{ url('/workerAjax/post') }}",
type: 'POST',
data: {
firstName: jQuery('#firstName').val(),
lastName: jQuery('#lastName').val(),
email: jQuery('#email').val(),
password: jQuery('#password').val(),
},
success: function(result){
jQuery('.alert').show();
jQuery('.alert').html(result.success);
}});
});
});
</script>
答案 1 :(得分:0)
在命名空间声明之后您缺少导入请求类:
use Illuminate\Http\Request;
没有它,Laravel不知道如何注入该参数,也不注入任何东西。
答案 2 :(得分:0)
因此,使用命令ssh php artisan -V
,我发现我一直在使用Laravel 4.2而不是Laravel 6.15,因为我的整体安装使我相信。
我通过直接从路由调用控制器功能解决了这个问题:
Route::post('/workerAjax/{post}', function(){
return WorkerController::storeWorker(Input::all());
});
和WorkerController.php
<?php
class WorkerController extends \BaseController {
public function storeWorker($request){
$val = json_encode($request);
// use for accessing json variables
$firstName = $request['firstName'];
// alert.(result.success) returns:
// {"firstName":"John","lastName":"Doe","email":"johndoe@email.com","password":"password1"}
return Response::json(['success'=>$val],200);
}
?>
感谢大家的投入,希望这对以后的人有所帮助!