I';在尝试使用jQuery和ajax提交表单时,我在RouteCollection.php中得到了MethodNotAllowedHttpException
正如本主题中提到的,在尝试使用jQuery和ajax提交表单时,我在RouteCollection.php中得到了MethodNotAllowedHttpException 我在routes.php文件中尝试了以下内容I';在尝试使用jQuery和ajax提交表单时,我在RouteCollection.php中得到了MethodNotAllowedHttpException,jquery,ajax,laravel-5.2,Jquery,Ajax,Laravel 5.2,正如本主题中提到的,在尝试使用jQuery和ajax提交表单时,我在RouteCollection.php中得到了MethodNotAllowedHttpException 我在routes.php文件中尝试了以下内容 Route::post('/send/', 'CommsController@send'); Route::post('/send', 'CommsController@send'); 及 控制器 use App\Http\Controllers\Controller; c
Route::post('/send/', 'CommsController@send');
Route::post('/send', 'CommsController@send');
及
控制器
use App\Http\Controllers\Controller;
class CommsController extends Controller
{
protected function send(Request $request);
{
return response($request);
}
}
public function send(Request $request)
{
// return view('contact.contact');
return view('contact.contact')->with('data', json_encode($request));
}
jQuery ajax
if ( isValid ) {
$( ".validation-summary" ).html( "Sending message..." );
var data = "";
data = "message_type=contact"
+ "&first_name=" + first_name.val()
+ "&last_name=" + last_name.val()
+ "&email=" + email.val()
+ "&message=" + message.val()
$.ajax({
type: "post",
url: "send",
data: data,
error: function( response ) {
$( ".validation-summary" ).removeClass( "success" );
$( ".validation-summary" ).addClass( "validation-summary error" );
$( ".validation-summary" ).html( "Error" );
},
success: function() {
$( ".validation-summary" ).html( "Message sent." );
}
});
return false;
} else {
return false;
}
<head>
<meta id="csrf_token" name="csrf_token" content="{{ csrf_token() }}">
</head>
$( document ).ready(function() {
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
....
$.ajax({
type: "POST",
url: postAction,
data: JSON.stringify(postData),
dataType: 'json',
encode: true
}).done(function(response) {
// log data to the console so we can see
console.log(response);
// here we will handle errors and validation messages
if ( ! response.success ) {
valSum.removeClass( "success" );
valSum.addClass( "validation-summary error" );
valSum.html( response.errors );
} else {
// $( ".validation-summary" ).html( "Message sent." );
valSum.html( response.message );
}
})
.fail(function(response) {
valSum.removeClass( "success" );
valSum.addClass( "validation-summary error" );
valSum.html( "Server Error: " + response.statusText + " processing your request, please contact Dorothea or report a bug." );
});
谢谢。在Laravel 5.2中,您需要在
web
中间件中设置路由,我假设您已经将post
路由放置在中间件中:
Route::group(['middleware' => ['web']], function() {
Route::post('/send', 'YourController@methodName');
});
如果我在您的位置,我会做一些其他的事情,尽管与您的代码所做的类似
jQuery
(function() {
// replace $('.btnSubmit') with whatever you want
$('.btnSubmit').click(function(e) {
var form = $('#yourFormId'),
postAction = form.attr('action'),
postData = form.serialize();
$.ajax({
type: 'POST',
url: postAction,
data: postData,
success: function(res) {
console.log(res);
},
error: function(res) {
console.log('Error:' + res);
}
});
return false;
});
})();
我所做的:
$('.validation summary')
5次。这意味着您违反了不推荐的。解决方案:
var valSum = $('.validation-summary');
然后在中,您可以在该代码段中使用它的次数
希望这对你有所帮助。快乐编码。干杯。在Laravel 5.2中,您需要在
web
中间件中设置路由,我假设您已经将post
路由放置在中间件中:
Route::group(['middleware' => ['web']], function() {
Route::post('/send', 'YourController@methodName');
});
如果我在您的位置,我会做一些其他的事情,尽管与您的代码所做的类似
jQuery
(function() {
// replace $('.btnSubmit') with whatever you want
$('.btnSubmit').click(function(e) {
var form = $('#yourFormId'),
postAction = form.attr('action'),
postData = form.serialize();
$.ajax({
type: 'POST',
url: postAction,
data: postData,
success: function(res) {
console.log(res);
},
error: function(res) {
console.log('Error:' + res);
}
});
return false;
});
})();
我所做的:
$('.validation summary')
5次。这意味着您违反了不推荐的。解决方案:
var valSum = $('.validation-summary');
然后在中,您可以在该代码段中使用它的次数
希望这对你有所帮助。快乐编码。干杯。在routes.php和ajax调用中尝试使用“POST”而不是“POST”。我也有同样的问题,这个可以帮我解决。希望有帮助。在routes.php和ajax调用中尝试使用“POST”而不是“POST”。我也有同样的问题,这个可以帮我解决。希望能有所帮助。根据您对
csrf\u代币的最后评论,最佳解决方案(IMO)是:
将以下行添加到内部
:
这意味着csrf总是自动包含在ajax调用中
或者,您可以只获取\u token
的值,并将其包含在数据变量中,如下所示:
data = "message_type=contact"
+ "&first_name=" + first_name.val()
+ "&last_name=" + last_name.val()
+ "&email=" + email.val()
+ "&message=" + message.val()
+ "&_token=" + $("input[name=_token]").val();
希望这有帮助 根据您对csrf\u代币的最后评论
最佳解决方案(IMO)是:
将以下行添加到内部
:
这意味着csrf总是自动包含在ajax调用中
或者,您可以只获取\u token
的值,并将其包含在数据变量中,如下所示:
data = "message_type=contact"
+ "&first_name=" + first_name.val()
+ "&last_name=" + last_name.val()
+ "&email=" + email.val()
+ "&message=" + message.val()
+ "&_token=" + $("input[name=_token]").val();
希望这有帮助 更新:my routes.php文件
Route::post('/send/', 'CommsController@send');
Route::post('/send', 'CommsController@send');
控制器
use App\Http\Controllers\Controller;
class CommsController extends Controller
{
protected function send(Request $request);
{
return response($request);
}
}
public function send(Request $request)
{
// return view('contact.contact');
return view('contact.contact')->with('data', json_encode($request));
}
app/Http/Middleware/VerifyCsrfToken.php//VerifyCsrfToken.php中的这个添加可能没有必要,但也不太确定
protected function tokensMatch($request) {
// If request is an ajax request, then check to see if token matches token provider in
// the header. This way, we can use CSRF protection in ajax requests also.
$token = $request->ajax() ? $request->header('X-CSRF-Token') : $request->input('_token');
return $request->session()->token() == $token;
}
jQuery ajax
if ( isValid ) {
$( ".validation-summary" ).html( "Sending message..." );
var data = "";
data = "message_type=contact"
+ "&first_name=" + first_name.val()
+ "&last_name=" + last_name.val()
+ "&email=" + email.val()
+ "&message=" + message.val()
$.ajax({
type: "post",
url: "send",
data: data,
error: function( response ) {
$( ".validation-summary" ).removeClass( "success" );
$( ".validation-summary" ).addClass( "validation-summary error" );
$( ".validation-summary" ).html( "Error" );
},
success: function() {
$( ".validation-summary" ).html( "Message sent." );
}
});
return false;
} else {
return false;
}
<head>
<meta id="csrf_token" name="csrf_token" content="{{ csrf_token() }}">
</head>
$( document ).ready(function() {
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
....
$.ajax({
type: "POST",
url: postAction,
data: JSON.stringify(postData),
dataType: 'json',
encode: true
}).done(function(response) {
// log data to the console so we can see
console.log(response);
// here we will handle errors and validation messages
if ( ! response.success ) {
valSum.removeClass( "success" );
valSum.addClass( "validation-summary error" );
valSum.html( response.errors );
} else {
// $( ".validation-summary" ).html( "Message sent." );
valSum.html( response.message );
}
})
.fail(function(response) {
valSum.removeClass( "success" );
valSum.addClass( "validation-summary error" );
valSum.html( "Server Error: " + response.statusText + " processing your request, please contact Dorothea or report a bug." );
});
php_error.log文件条目
local.ERROR: Illuminate\Session\TokenMismatchException in C:\cx\laravel\dorothea\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\VerifyCsrfToken.php:67
更新:my routes.php文件
Route::post('/send/', 'CommsController@send');
Route::post('/send', 'CommsController@send');
控制器
use App\Http\Controllers\Controller;
class CommsController extends Controller
{
protected function send(Request $request);
{
return response($request);
}
}
public function send(Request $request)
{
// return view('contact.contact');
return view('contact.contact')->with('data', json_encode($request));
}
app/Http/Middleware/VerifyCsrfToken.php//VerifyCsrfToken.php中的这个添加可能没有必要,但也不太确定
protected function tokensMatch($request) {
// If request is an ajax request, then check to see if token matches token provider in
// the header. This way, we can use CSRF protection in ajax requests also.
$token = $request->ajax() ? $request->header('X-CSRF-Token') : $request->input('_token');
return $request->session()->token() == $token;
}
jQuery ajax
if ( isValid ) {
$( ".validation-summary" ).html( "Sending message..." );
var data = "";
data = "message_type=contact"
+ "&first_name=" + first_name.val()
+ "&last_name=" + last_name.val()
+ "&email=" + email.val()
+ "&message=" + message.val()
$.ajax({
type: "post",
url: "send",
data: data,
error: function( response ) {
$( ".validation-summary" ).removeClass( "success" );
$( ".validation-summary" ).addClass( "validation-summary error" );
$( ".validation-summary" ).html( "Error" );
},
success: function() {
$( ".validation-summary" ).html( "Message sent." );
}
});
return false;
} else {
return false;
}
<head>
<meta id="csrf_token" name="csrf_token" content="{{ csrf_token() }}">
</head>
$( document ).ready(function() {
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
....
$.ajax({
type: "POST",
url: postAction,
data: JSON.stringify(postData),
dataType: 'json',
encode: true
}).done(function(response) {
// log data to the console so we can see
console.log(response);
// here we will handle errors and validation messages
if ( ! response.success ) {
valSum.removeClass( "success" );
valSum.addClass( "validation-summary error" );
valSum.html( response.errors );
} else {
// $( ".validation-summary" ).html( "Message sent." );
valSum.html( response.message );
}
})
.fail(function(response) {
valSum.removeClass( "success" );
valSum.addClass( "validation-summary error" );
valSum.html( "Server Error: " + response.statusText + " processing your request, please contact Dorothea or report a bug." );
});
php_error.log文件条目
local.ERROR: Illuminate\Session\TokenMismatchException in C:\cx\laravel\dorothea\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\VerifyCsrfToken.php:67
您好@user3514160我只是想让您知道,我通过将post方法更改为get方法解决了我的问题。不太清楚get为什么起作用,但post不起作用,但至少现在这个问题已经解决了
如果有人能解释一下为什么会出现这种情况,那就太好了。Hi@user3514160我只是想让你知道,我通过将post方法更改为get方法解决了我的问题。不太清楚get为什么起作用,但post不起作用,但至少现在这个问题已经解决了
如果有人能解释为什么会是这样的情况,那么就好了。使用浏览器开发者工具的“网络”选项卡,您可以确认它肯定是用正确的HTTP方法发送到正确的端点吗?我得到405(不允许方法)异常,如果routes.php是补丁,如果设置为post,则为500。不过,不太确定为什么。你的php错误日志说什么?谢谢你甚至没有考虑检查php\u error.log文件。最重要的日志条目可能是VerifyCsrfToken.php:67中的local.ERROR:illighted\Session\tokenmischException,因此我添加了{!!csrf_field()!!}到我的表单,但它仍然抛出相同的错误。使用浏览器开发工具的“网络”选项卡,您可以确认它确实使用正确的HTTP方法发送到了正确的端点吗?如果routes.php是patch,我会收到405(不允许使用方法)异常,如果它设置为post,则会收到500。不太清楚为什么。你的php_error.log是怎么说的?谢谢,我甚至没有想过检查php_error.log文件。在VerifyCsrfToken.php:67中,最重要的日志条目可能是local.ERROR:illumb\Session\TokenMismatchException,因此我在表单中添加了{!!csrf_field()!!},但它仍然抛出相同的错误。您好@MasterSith,我可能弄错了,但我认为您不需要在routes.php中将“post”更改为“post”,但是你说我应该在我的ajax调用中进行更改可能是对的。虽然这不是问题所在,但非常感谢您的帮助。您好@MasterSith,我可能弄错了,但我认为您不需要在routes.php中将“post”更改为“post”,但您可能说我应该在ajax调用中更改。这