尝试使用jQuery和Ajax提交表单时,RouteCollection.php中出现MethodNotAllowedHttpException [英] I'm getting a MethodNotAllowedHttpException in RouteCollection.php when trying to submit a form using jQuery and ajax

查看:194
本文介绍了尝试使用jQuery和Ajax提交表单时,RouteCollection.php中出现MethodNotAllowedHttpException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如本主题所述,当尝试使用jQuery和Ajax提交表单时,我在RouteCollection.php中获得了MethodNotAllowedHttpException

As mentioned in the subject I'm getting a MethodNotAllowedHttpException in RouteCollection.php when trying to submit a form using jQuery and ajax

我已经在我的route.php文件中尝试了以下内容

I've tried the following in my routes.php file

Route::post('/send/', 'CommsController@send');

Route::patch('/send/', 'CommsController@send');

控制器

use App\Http\Controllers\Controller;

class CommsController extends Controller
{
    protected function send(Request $request);
    {
        return response($request);
    }
}

jQuery ajax

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;
}

谢谢.

推荐答案

您好@ user3514160我只是想让您知道,我通过更改post方法获取方法解决了我的问题.不太确定为什么get可以正常运行,但post却不能正常运行,但至少现在这个问题已经解决了.

Hi @user3514160 I just wanted to let you know that I solved my problem by changing the post method to get methods. Not too sure why get is working but post isn't but for now at least the issue has been resolved.

如果任何人都可以弄清楚为什么会如此,那将是很好的事情.

If anyone can shed some light as to why this would be the case that would be great.

这篇关于尝试使用jQuery和Ajax提交表单时,RouteCollection.php中出现MethodNotAllowedHttpException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆