Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将数据从ajax传递到Laravel5.1控制器,以便保存到mysql?_Php_Jquery_Mysql_Ajax_Laravel - Fatal编程技术网

Php 将数据从ajax传递到Laravel5.1控制器,以便保存到mysql?

Php 将数据从ajax传递到Laravel5.1控制器,以便保存到mysql?,php,jquery,mysql,ajax,laravel,Php,Jquery,Mysql,Ajax,Laravel,我需要将数据从jquery(版本1.9.1)传递到我的控制器(Laravel5.1),然后将其保存到mysql 如何做到这一点并传递变量槽?到目前为止还不起作用。如需了解更多详情,请询问我 jquery: $(".tic").click(function(){ var slot = $(this).attr('id'); playerTurn(turn, slot); $.ajax({ url: '/addhistory', type:

我需要将数据从jquery(版本1.9.1)传递到我的控制器(Laravel5.1),然后将其保存到mysql

如何做到这一点并传递变量槽?到目前为止还不起作用。如需了解更多详情,请询问我

jquery:

 $(".tic").click(function(){
    var slot = $(this).attr('id');
    playerTurn(turn, slot);
    $.ajax({
        url: '/addhistory',
        type: 'POST',
        data: { _token: {{ csrf_token() }}, moves: slot },
        success: function()
        {
            alert("Data has been saved successfully!");
        }
    });
 });
控制器:

 public function addhistory(Request $request)
    {
        $history = new History();
        $history->game_id = Game::orderBy('id', 'desc')->first()->id;
        $history->moves = $request->moves;
        $history->save();
        return redirect()->back();
    }
路线:

Route::post('/addhistory', 'GameController@addhistory');
控制台中的错误:

(index):198 Uncaught ReferenceError: HAmcYRScL9puItnUGbd2kHx.... is not defined
    at HTMLAnchorElement.<anonymous> ((index):198)
    at HTMLAnchorElement.dispatch (191.js:3)
    at HTMLAnchorElement.v.handle (191.js:3)
(索引):198未捕获引用错误:HAmcYRScL9puItnUGbd2kHx。。。。没有定义
在兰开夏。((索引):198)
在htmlanchorement.dispatch(191.js:3)
在htmlanchorement.v.handle(191.js:3)


191.js文件是1.9.1的jquery版本

您可以使用此代码,它可能会工作

 $(".tick").click(function (event) {
    event.preventDefault();
    $('.loading').show();
    var form = $(this);
    var data = new FormData($(this)[0]);
    var url = form.attr("action");
    $.ajax({
        type: "POST",
        url: url,
        data: data,
        async: false,
        cache: false,
        contentType: false,
        processData: false,
        success: function (data) {
           alert("Data has been saved successfully.");
        },
        error: function (xhr, textStatus, errorThrown) {
            alert(errorThrown);
        }
    });
    return false;
});

您可以使用此代码,它可能会起作用

 $(".tick").click(function (event) {
    event.preventDefault();
    $('.loading').show();
    var form = $(this);
    var data = new FormData($(this)[0]);
    var url = form.attr("action");
    $.ajax({
        type: "POST",
        url: url,
        data: data,
        async: false,
        cache: false,
        contentType: false,
        processData: false,
        success: function (data) {
           alert("Data has been saved successfully.");
        },
        error: function (xhr, textStatus, errorThrown) {
            alert(errorThrown);
        }
    });
    return false;
});
试试这个代码-

$(".tic").click(function(){
  var slot = $(this).attr('id');
  var token= $("input[name='_token']").val();
  playerTurn(turn, slot);
  $.ajax({
      url: '/addhistory',
      type: 'POST',
      data: { '_token': token, 'moves': slot },
      success: function()
      {
          alert("Data has been saved successfully!");
      }
  });
});
在控制器中,您不需要返回重定向,因为请求是异步的。所以我回到了现实。确保在控制器顶部包含历史模型

 public function addhistory(Request $request)
    {
        $game_id=Game::orderBy('id', 'desc')->first()->id;
        History::create([
                        'game_id'=>$game_id,
                        'moves'=>$request->moves
        ]);
        return true;
    }
试试这个代码-

$(".tic").click(function(){
  var slot = $(this).attr('id');
  var token= $("input[name='_token']").val();
  playerTurn(turn, slot);
  $.ajax({
      url: '/addhistory',
      type: 'POST',
      data: { '_token': token, 'moves': slot },
      success: function()
      {
          alert("Data has been saved successfully!");
      }
  });
});
在控制器中,您不需要返回重定向,因为请求是异步的。所以我回到了现实。确保在控制器顶部包含历史模型

 public function addhistory(Request $request)
    {
        $game_id=Game::orderBy('id', 'desc')->first()->id;
        History::create([
                        'game_id'=>$game_id,
                        'moves'=>$request->moves
        ]);
        return true;
    }

您是否尝试在
{{csrf_token()}}
周围放置单引号?尝试
{csrf_token:{{csrf_token()}}}
。因为CSRF可能包含一些算术运算符,JavaScript尝试对其求值。500(内部服务器错误)我用这个得到了这个错误。你能给堆栈跟踪显示一个得到精确的错误吗?你的脚本在客户端运行得很好。请检查服务器端的错误。您可以在
storage/log/laravel.log
中看到它。您是否尝试在
{{csrf_-token()}}
周围放置单引号?尝试
{csrf_-token()}}}
。因为CSRF可能包含一些算术运算符,JavaScript尝试对其求值。500(内部服务器错误)我用这个得到了这个错误。你能给堆栈跟踪显示一个得到精确的错误吗?你的脚本在客户端运行得很好。请检查服务器端的错误。您可以在
storage/log/laravel.log