【问题标题】:Laravel route not working when posting data via JQuery通过 JQuery 发布数据时 Laravel 路由不起作用
【发布时间】:2019-07-29 10:40:51
【问题描述】:

我正在尝试使用 jquery 验证我的表单。提交时,Laravel 在 web.php 中找不到路由。当我之前只使用 model::Form 时,它起作用了.. 尝试了很多东西,但没有一个给我解决方案

编辑:419 执行已消失:现在面临 500 内部错误

web.php:

Route::post('/profile/sendmail', 'VendorController@send_mail')->name('profile.sendmail');

供应商控制器:

public function send_mail(Request $request){
        dd("test");
    }

contact.blade.php

<div class="events single-event">
    <div class="o-grid">
        <div class="o-grid__col u-6/12">
            <div class="o-grid__col u-12/12@sm">
                <h4>@lang('profile.contactTitle')</h4>
            </div>


            <div class="o-grid__col u-12/12@sm" id="alert_modal" style="margin-top: 150px; display: none">
                <div class="modal-body">
                    <h3>Alert</h3>
                    <h5 id="alert_message"></h5>
                </div>
                <input type="hidden" id="profile_id" value="{{ $profile->id }}">
            </div>


            <div class="o-grid__col u-12/12@sm">
                {!! Form::label('', __('profile.contactSalutation').'*') !!}
                @if( Session::get('urlLang') == "en" )
                    {!! Form::select(__('contact_contactSalutation'), array('Miss' => 'Miss', 'Sir' => 'Sir'),array('class' => 'c-dropdown c-dropdown__simple u-mb-x6'),['required' => 'required']) !!}
                @else
                    {!! Form::select(__('contact_contactSalutation'), array('Frau' => 'Frau', 'Herr' => 'Herr'),array('class' => 'c-dropdown c-dropdown__simple u-mb-x6'),['required' => 'required']) !!}
                @endif
            </div>
            <br>
        </div>
        <div class="o-grid__col u-6/12">
            <div class="o-grid__col u-12/12@sm">
                <p style="color: #696978; font-size: 14px; text-align: right">@lang('profile.mandatoryField')</p>
            </div>
        </div>
    </div>
    <div class="o-grid">
        <div class="o-grid__col u-6/12">
            <div class="o-grid__col u-12/12@sm">
                {!! Form::label('contact_first_name', __('profile.contactFirstName').'*') !!}
                {!! Form::text('contact_first_name', null, ['placeholder' => __('profile.contactFirstName'),'class' => 'c-input required','id' => 'contact_first_name','required']) !!}
            </div>
            <div class="o-grid__col u-12/12@sm">
                {!! Form::label('contact_e_mail', __('profile.contactEmail').'*') !!}
                {!! Form::text('contact_e_mail', null, ['placeholder' => __('profile.contactEmail'),'class' => 'c-input required email','id' => 'contact_e_mail','required']) !!}
                <span class="text-danger">{{ $errors->first('contact_e_mail') }}</span>
            </div>
        </div>
        <div class="o-grid__col u-6/12">
            <div class="o-grid__col u-12/12@sm">
                {!! Form::label('contact_last_name', __('profile.contactLastName').'*') !!}
                {!! Form::text('contact_last_name', null, ['placeholder' => __('profile.contactLastName'),'class' => 'c-input required','id' => 'contact_last_name','required']) !!}
            </div>
            <div class="o-grid__col u-12/12@sm">
                {!! Form::label('contact_phone', __('profile.contactPhone')) !!}
                {!! Form::text('contact_phone', null, ['placeholder' => __('profile.contactPhone'),'class' => 'c-input','id' => 'contact_phone']) !!}
            </div>
        </div>

        <div class="o-grid__col u-12/12">
            <div class="o-grid__col">
                {!! Form::label('text', __('profile.contactMessageInfo')) !!}
                {!! Form::textarea('contact_text',null,['class' => 'c-input c-input__text required','placeholder' => __('profile.contactMessageInfo'),'id' => 'contact_text','required']) !!}
            </div>
        </div>

        <div class="o-grid__col u-12/12">
            <input class="c-input__checkbox required" id="toc" type="checkbox" name="agree_to_toc" value="1" required>
            <label class="c-input__checkboxLabel p2" for="toc">
                {!! @trans('global.formTacInfo', [
                    'class' => 'c-link c-link__primary',
                    'link_datenschutz' => route('static.show', ['folder_id' => 28, 'slug' => "datenschutz"])
                    ]) !!}</label>
        </div>

        <div class="o-grid__col u-text-right">
            <button id="submit_contact_form" class="c-btn c-btn--small c-btn--red" type="submit" style="display: none" onclick="submitForm()">
                <span>@lang('profile.contactSendMessage')</span></button>
        </div>
    </div>
</div>

<script>
    $.ajaxSetup({
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        }
    });
    $('#toc').change(function() {
        if($(this).is(":checked")) {
            $('#submit_contact_form').show();
        }
        else{
            $('#submit_contact_form').hide();
        }
    })

    function submitForm() {
        var profile_id = $("#profile_id").val();
        $.ajax({
            method: 'POST',
            url: '{{route("profile.sendmail")}}',
            data: {profile_id: profile_id},
            dataType: "json",
            success: function(data){
                $('#alert_message').text(data.message);
                $('#alert_modal').show();
            },
            error: function(data){
                $('#alert_message').text(data.message);
                $('#alert_modal').show();
            }
        });
    }
</script>

【问题讨论】:

    标签: php jquery laravel


    【解决方案1】:

    419 状态通常与令牌问题有关。

    尝试更改'_token'

    到这里:

    headers: {
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
      }
    

    【讨论】:

    • 是的,这是我经常看到的,但后来我得到:VM322:14 Uncaught SyntaxError: Unexpected token : at m (jquery.min.js:2) at Re (jquery.min.js :2) 在 w.fn.init.append (jquery.min.js:2) 在 w.fn.init. (jquery.min.js:2) 在 z (jquery.min.js:2)在 w.fn.init.html (jquery.min.js:2) 在 Object.success (contact-provider.js:19) 在 u (jquery.min.js:2) 在 Object.fireWith [as resolveWith] ( jquery.min.js:2) 在 k (jquery.min.js:2)
    • @belgiums ,你不能缩小你的js并显示错误报告的位置吗?在哪一行。
    • 你的意思是 jquery.min.js?它只是基本的js文件吗?我自己的js有问题
    • 在 ajax 请求中:headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') },
    • 419 错误消失了。我把它作为第一个 js 脚本: $.ajaxSetup({ headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') } });现在有一个 500 异常。试图解决它:)
    【解决方案2】:

    在您的发布路线上,您不需要在 URL 中传递变量:

    将您的路线更改为:

    Route::post('/profile/sendmail', 'VendorController@send_mail')->name('profile.sendmail');
    

    还有ajax请求:

        $.ajax({
                        method: 'POST',
                        headers: {
                        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                        },
                        url: '{{route("profile.sendmail")}}',
                        data: {profile_id_var: profile_id_var},
    

    在您的控制器上:

    $request->get('profile_id_var');
    

    【讨论】:

    • 这有点帮助,在我的标题中,他现在给出了“profile_id => 409”(409 = 数字)。但仍然获得 419 状态.. 谢谢!
    • 没有到达控制器?检查'send_mail()'函数
    • no.. 第一行是 dd("test")。这是他: public function send_mail(Request $request,VendorProfile $profile_id){ dd("f"); }
    • 试试 send_mail(Request $request){ dd("f"); }
    • 419 现在由于 x-csrf 标头而消失了。我也试过控制器,现在有 500 个内部错误..
    【解决方案3】:

    我认为您的行 data: profile_id_var 不正确,因为您使用 profile_id 参数(而不是 profile_id_var)定义了您的路线。

    如果您尝试data: { profile_id: profile_id_var } 会怎样?

    【讨论】:

      【解决方案4】:
      $.ajax({
            url: '/post-comments',// the right way;
            url: 'post-comments' // the wrong way because its call the route when we are and not the route when we need to send.
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-11-17
        • 1970-01-01
        • 2018-02-13
        • 1970-01-01
        • 2021-01-13
        • 2018-12-25
        • 2019-10-28
        • 2016-11-05
        相关资源
        最近更新 更多