-1

I'm having a trouble with an Ajax request, I want to post some data using jQuery. I have to say that before with GET it works fine, but I have a lot of data to send and I get: (Request-URI Too Long) so, for that I'm changing to post.

I'm working on Laravel, I changed the route from get to post, the method in the controller is called correctly, but for some reason the ajax petition doesn't send the data (groups and segments).

Groups and segments are Javascript arrays, as I said, it works fine with GET but it fails when the data is too long.

My current code looks like this:

$.ajax({
   type: "POST",
   data: {groups:groups, segments:segments},
   url: "check",
   success: function(msg){ 
             //something... 
          }); 
  });

I tried too adding this lines:

contentType: 'application/json; charset=utf-8',

But I had the same result, I get error 500 when I try to read the variables in the controller.

My route in Laravel is:

Route::post('check',array('as'=>'check','uses'=>'FileController@checkfile'));

if I put return 'Hallo!';the text is returned as well, then, to see all the variables that are sended to the controller I made:

dd(Input::all());

but it prints an empty array. So, for what I see the problem is not the route but the way as I am sending the data with ajax Thanks!

marc_s
  • 675,133
  • 158
  • 1,253
  • 1,388
Sredny M Casanova
  • 3,578
  • 10
  • 47
  • 93

2 Answers2

1

Try adding a slash to your url:

$.ajax({
   type: "POST",
   data: {groups:groups, segments:segments},
   dataType: 'JSON',
   url: "/check",
   success: function(msg){ 
             //something... 
          }); 
});

Route::post('/check',array('as'=>'check','uses'=>'FileController@checkfile'));

It also seems that there is no CSRF token included with the ajax request. This doesn't matter if you are route is not using web middleware but since you didn't post your all routes.php you should make sure.

If you indeed using web group middleware you can include CSRF token in you header as a meta tag:

<meta name="csrf-token" content="{{ csrf_token() }}"/>

and pass it with your ajax request.

var CSRF_TOKEN = $('meta[name="csrf-token"]').attr('content');

$.ajax({
   type: "POST",
   data: {_token: CSRF_TOKEN, groups:groups, segments:segments},
   dataType: 'JSON',
   url: "/check",
   success: function(msg){ 
         //something...  
   }); 
}); 
Can Celik
  • 1,814
  • 18
  • 28
0

After try a lot of things I noticed that the post request in Laravel need to have attached the CSRF token, so I fix it adding this line in the HTML:

<meta name="csrf-token" content="{{ csrf_token() }}">

and then, in the Ajax request add the beforSend property, then the request will be like this:

$.ajax({
     type: "POST",
     data: {groups:groups, segments:segments},
     url: "check",
     async: true,
     beforeSend: function (request) {
                       return request.setRequestHeader('X-CSRF-Token', $("meta[name='csrf-token']").attr('content'));
                   },
     success: function(msg){ //something...});
});
Sredny M Casanova
  • 3,578
  • 10
  • 47
  • 93