jQuery 如何从laravel中的ajax请求中获取数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43474484/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 17:20:06  来源:igfitidea点击:

how to get data from ajax request in laravel

jqueryajaxlaravellaravel-5.4

提问by Muhammad Awais

I am doing ajax request and passing this data

我正在做 ajax 请求并传递这些数据

    $.ajax({
  url: "{{URL::to('match')}}/"+ id,
  type: 'PUT',
  // dataType: 'default: Intelligent Guess (Other values: xml, json, script, or html)',
  data: {
    match_id : id,
    start_time : newTime,
    competitionId: {{$cid}},
    _token:     '{{ csrf_token() }}'
  }
})

and in laravel trying to get this data as

并在 Laravel 中尝试将这些数据作为

dd($request->start_time);

but it is not working i am getting null

但它不起作用我得到了空

In chrome developer tools data with ajax request sent correctly this is one simple

在 ajax 请求正确发送的 chrome 开发者工具数据中,这是一个简单的

match_id:1
start_time:03:00
competitionId:1
_token:9p8plPay7HLvJvMrTgxayEH74Ow6c2D1cli1yU01

all of this was working fine before I moved this site to a new server

在我将此站点移至新服务器之前,所有这些都运行良好

have i missed any file ?

我错过了任何文件吗?

回答by Muhammad Awais

It works fine after i changed type to Post and then added a field _method: PUT i.e

在我将类型更改为 Post 然后添加一个字段 _method: PUT 后,它工作正常,即

$.ajax({
      url: "{{URL::to('match')}}/"+ id,
      type: 'POST',
      // dataType: 'default: Intelligent Guess (Other values: xml, json, script, or html)',
      data: {
        _method: 'PUT',
        match_id : id,
        start_time : newTime,
        competitionId: {{$cid}},
        _token:     '{{ csrf_token() }}'
      }
    })

回答by Genina Anne Gabuten

type php artisan route:list

输入 php artisan route:list

check your route there for example your

检查您在那里的路线,例如您的

Method = put

方法=放置

Uri = match/{match}

Uri = 匹配/{匹配}

Name = match.update

名称 = match.update

Action = App\Http\Controllers\MatchController@update//your method

Action = App\Http\Controllers\MatchController@ update//你的方法

Route:

路线:

Route::resource('/match', 'MatchController');

this is your ajax call:

这是你的ajax调用:

$.ajax({
    url: 'match/'+ id, //this is your uri
    type: 'PUT', //this is your method
    data: { match_id:id, start_time:newTime },
    dataType: 'json',
    success: function(response){

    }
});

Your Controller:

您的控制器:

public function update(Request $request, $match_id)
{
   if(request()->ajax()){
      $match = Match::find($match_id);
      $validator = Validator::make($request->all(), [
         'start_time'=>'required',
      ]);

      if($validator->passes()) 
      {
        $match->start_time = $request->start_time;
        $match->save();

        return response()->json(['msg'=>'Updated Successfully', 'success'=>true]);
      }
      return response()->json(['msg'=>$validator->errors()->all()]);
    }
}