jQuery不响应POST请求

时间:2018-11-18 22:59:35

标签: jquery laravel

我正在使用Laravel和JQuery,我已经设置了一个表格和用于将其信息存储在数据库中的控制器指令,现在的问题是,当我单击“提交”按钮后全部设置完毕,什么也没发生,我始终保持控制台打开状态以检查错误并查看请求,但是现在它什么也没做。

代码如下:

在标题上我添加了这个:

<script src="http://code.jquery.com/jquery-3.3.1.min.js"
        integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
        crossorigin="anonymous">
</script>

我的表单如下:

<form id="myForm">
    <div class="form-group label-floating">
        <label class="control-label" style="margin-top: -5px;">Title</label>
        <input class="form-control" id="title" type="text" required="">
    </div>
    <div class="form-group label-floating">
        <label class="control-label" style="margin-top: -5px;">Summary</label>
        <textarea id="summary" class="form-control" required=""></textarea>
    </div>
    <div class="form-group label-floating">
        <label class="control-label" style="margin-top: -5px;">Description</label>
        <textarea id="description" class="form-control" required=""></textarea>
    </div>
    <div class="form-group label-floating">
        <label class="control-label" style="margin-top: -5px;">Link to media (optional)</label>
        <input id="medialink" class="form-control" type="text" required="">
    </div>
    <div class="col-lg-12 col-md-6 col-sm-12 col-xs-12">
        <button class="btn btn-primary btn-lg full-width" type="button" id="ajaxSubmit">Submit</button>
    </div>
</form>

,控制器如下所示:

public function submitit($id, Request $request)
{
    $bounty = Bounty::where('id', $id)->first();
    $vulnerability = new Vulnerability();
    $vulnerability->title = $request->title;
    $vulnerability->summary = $request->summary;
    $vulnerability->description = $request->description;
    $vulnerability->medialink = $request->media;
    $vulnerability->save();
    Toastr::success('Successfuly submitted', 'Congratulations!', ['toast-top-right']);
    return response()->json(['success'=>'Submitted correctly']);
}

Ajax:

<script type="text/javascript">

  jQuery(document).ready(function(){
            jQuery('#ajaxSubmit').click(function(e){
               $.ajaxSetup({
                  headers: {
                      'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
                  }
              });
               jQuery.ajax({
                  url: "{{ url('/submitit/' . $bounty->id . '/' . $bounty->company_identifier) }}",
                  method: 'post',
                  data: {
                     _token: '{{csrf_token()}}',
                     title: jQuery('#title').val(),
                     summary: jQuery('#summary').val(),
                     description: jQuery('#description').val(),
                     medialink: jQuery('#medialink').val()
                  },
                  success: function(result){
                     console.log(result);
                  }});
               });
            });

</script>

在我的路线上,我得到了:

Route::post('/submitit/{id}', [

'uses' => 'BountyController@submitit',

])->middleware('auth');

这是怎么回事?为什么不发送任何请求?

4 个答案:

答案 0 :(得分:0)

找到了解决方案,我没有注意到ajax上的路由与web.php文件中的路由不匹配。

解决方案是更改此设置:url: "{{ url('/submitit/' . $bounty->id . '/' . $bounty->company_identifier) }}",

对此:url: "{{ url('/submitit/' . $bounty->id) }}",

答案 1 :(得分:0)

您更改网址

 url: "{{ url('/submitit/' . $bounty->id . '/' . $bounty->company_identifier) }}",

 url: "{{ action('BountyController@submitit', ['id' => $bounty->id]) }}",

然后转储到Controller或F12打开选项卡“网络”中,查看请求发送

答案 2 :(得分:0)

 jquery ajax 
      url: "{{ url('/submitit/' . $bounty->id . '/' . $bounty->company_identifier) }}" 
   write In route url like

   Route::post('/submitit/{id}/{company_identifier}',
      'BountyController@submitit')->middleware('auth');

答案 3 :(得分:-2)

尝试在ajax设置对象中使用Type代替method

更多答案: 设置时

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

您在_token字段中不需要data。 尝试将此部分添加到ajax设置对象中,以便查看是否有任何错误响应。

error: function (res) {
        console.log(res);
    }