Edo
Edo

Reputation: 87

Laravel 5.6 ajax request, can't read the value

I can't read the values send with ajax request, i don't know what im missing.

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

    $("#send").click(function(e){

        e.preventDefault();
        var id = 5;

        $.ajax({
            type:'POST',
            url:'/device/',
            data:{id:id},
            success:function(data){
                console.log('Yes' + data);
            }
        });
});

in the controller device i just dump the input from the ajax call

    $data = $request->all(); 
    dd($data); 

I can't see the value send via ajax in the console, i only see Yes but not the data. What im missing ?

Upvotes: 0

Views: 220

Answers (5)

Yugma Patel
Yugma Patel

Reputation: 627

Just Replace the + with , like below

 console.log('Yes' , data);

Upvotes: 1

Payal Pandav
Payal Pandav

Reputation: 139

try this one.

in controller:

return $data;

And simply replace + with, like this:

console.log('Yes' , data);

Upvotes: 0

Rob
Rob

Reputation: 164

Use This:

$("#send").click(function(e){

    e.preventDefault();
    var id = 5:

    $.ajax({
        type:'POST',
        url:'{{url('/device')}}',
        data:{
             'ajax': 1,'id':id,'_token': {{csrf_token}}
             },
        success:function(data){
            console.log('Yes'+data.id);
        }
    });

});

In the controller use:

public function name()
{
  if(Input::get('ajax') == 1)
  {
   print_r('Ajax is working');die;
  }
}

Also make sure, in the route you have post or any and not get for this route.

Upvotes: 0

Harshavardhan
Harshavardhan

Reputation: 382

try this in controller

public function store(Request $request) { return Response::json($request->all(), 200); }

dont forget to include "use Response; use Illuminate\Http\Request;"

Upvotes: 0

Sabyasachi Ghosh
Sabyasachi Ghosh

Reputation: 1500

use the following line in your controller;

return $data; instead of dd($data);

Upvotes: 2

Related Questions