1

This is my ajax request and i'm trying to pass those values into a controller in laravel

var deviceid="<?php echo $id; ?>";
var day="<?php echo $day; ?>";

$.ajax({

    'async': false,
    'global': false,
    url: '/location/show/getLocation/{id}/{date}',
    dataType: 'json',
    type: 'GET',
    data: { id:deviceid,date:day},

    success:function(data){
        myVariable=data;
        console.log(data);
    }
});

In my controller i got these values as

$id=$_GET['id'];
$date =$_GET['date'];
echo $id.$date;

But values doesn't show in console can anyone tell me the issue??

edited code

view

<script>  
var deviceid="<?php echo $id; ?>";
var day     ="<?php echo $day; ?>";
// console.log(deviceid,day);
$.ajax({

    'async': false,
    'global': false,
    url: '/location/show/getLocation',
    dataType: 'json',
    type: 'POST',
    data: { id:deviceid,date:day},

    success:function(data){
        console.log(data);
    }
});
</script>

controller

public function getLocation(Request $request)
{       
$id=$request->input('id');
$date=$request->input('date');
echo $id;
echo $date; exit;
}

route

Route::post('location/show/getLocation', 'DemoController@getLocation');
3
  • 2
    The url should be url: '/location/show/getLocation/' + deviceid + '/' + day, And you do not need the data key in this case. Commented Feb 18, 2016 at 6:18
  • Route::any('/location/show/getLocation/{id}/{date}', ''); this is my route path i tried what you said but it gave me 500 (Internal Server Error) Commented Feb 18, 2016 at 6:25
  • If you have debug set to true in Laravel, you should easily be able to trace out the exact reason for the error - including in most cases, the offending source file as well as the line number where the error originated. In your browser's console, go to the network tab, and checkout the response received for the above AJAX request. Commented Feb 18, 2016 at 7:28

1 Answer 1

1

in your ajax call...

var deviceid="<?php echo $id; ?>";
var day="<?php echo $day; ?>";

$.ajax({

'async': false,
'global': false,
url: '/location/show/getLocation',
dataType: 'json',
type: 'POST',
data: { id:deviceid,date:day},

success:function(data){
   var myVariable=data;
    console.log(data);
}
});

your route.php like

Route::post('location/show/getLocation/', 'demoController@getlocation');

define your controller name and function name which is you call on ajax

now in Controller add "getlocation function".

public function getlocation(Request $request)
{       
    $id=$request->input('id');
    $date=$request->input('date');
    echo $id;
    echo $date; exit;
 }

dont forgot to add use Illuminate\Http\Request; in Controller file if you dont add this line then Request method does not work.

if you want returning something so just do your result in jason decode which is response in ajax suceess then as you wish

Sign up to request clarification or add additional context in comments.

5 Comments

i did what like you said but gives 500 server error
you have define myVariable variable ? and check your script you should be mistake in syntax thats why it give internal server error
post full code in you original question so we can check whats problems
first you check your ajax is fire or not in browser consol....and i hope you changed the controller name i just for answer add "demoController" you can replace with your controller name....firstly open your browser consol go to network tab then fire your ajax and see what kind of error you face....please do debugging step by step i hope it will solve your problem

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.