简体   繁体   English

Ajax 值未传递给 Laravel 中的 controller

[英]Ajax value not being passed to the controller in Laravel

I'm trying to get a value from ajax request into my controller.我试图从 ajax 请求中获取一个值到我的 controller 中。 My js function shows desired value in alert but when I try to pass this value as data into the controller, the controller received null . My js function shows desired value in alert but when I try to pass this value as data into the controller, the controller received null .

I'm not sure if this is an error with my app logic or a different problem.我不确定这是我的应用程序逻辑错误还是其他问题。 I would appreciate any feedback.我将不胜感激任何反馈。

The form表格

<form>
   <input type="color" id="bgcolor" name="bgcolor">
   <button onclick="hex2rgb()">CLick</button>
</form>

The Javascript Javascript

<script>
    function hex2rgb(hex) {
        var hex = document.getElementById("bgcolor").value;
        r = hex.match(/^#([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})$/i);
        if (r) {
            return alert(r.slice(1, 4).map(function (x) {
                return parseInt(x, 16);

                let _token   = $('meta[name="csrf-token"]').attr('content');
                $.ajax({
                    url:"{{ route('niceActionController.multiStepStore') }}",
                    method:"POST",
                    data:{hex:hex,_token:_token},
                    success: function(response){ // What to do if we succeed
                      if(data == "success")
                          alert(response);
                    },
                    error: function(response){
                      alert('Error'+response);
                    }
                })

            }));
        }
        return null;
    }
</script>

The Controller Controller

public function multiStepStore(Request $request)
{
    $input = $request->get('hex');
    dd($input);
}

I think that you probles is in your token, the correct form to get a token for send in ajax is:我认为您的问题在于您的令牌,在 ajax 中获取发送令牌的正确形式是:

Blade

<form>
   <input type="color" id="bgcolor" name="bgcolor">
   <button onclick="hex2rgb()">CLick</button>
</form>

Javascript Javascript

<script>
      function hex2rgb() { //remove the data you are receiving
          var hex = document.getElementById("bgcolor").value;
          r = hex.match(/^#([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})$/i);
          if (r) {
              return alert(r.slice(1, 4).map(function (x) {
                  return parseInt(x, 16);

                  $.ajax({
                      url:"{{ route('niceActionController.multiStepStore') }}",
                      method:"POST",
                      data:{
                          _token: "{{ csrf_token() }}",
                          hex:hex,
                      },
                      success: function(response){ // What to do if we succeed
                          console.log(response);
                      },
                      error: function(response){
                          console.log('Error'+response);
                      }
                  })

              }));
          }
          return null;
      }
</script>

Controller Controller

public function multiStepStore(Request $request)
{
    dd($request->all());
}

Route路线

Route::post('/hex', 'Hexcontroller@multiStepStore')->name('niceActionController.multiStepStore');

to see the console.log you need to open your browser's inspector要查看 console.log,您需要打开浏览器的检查器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM