I am new to Laravel, even I am new to PHP. I am trying to access a controller function through AJAX, which will return some data. Every time when I run AJAX it shows 404 error. I am not sure what am I doing wrong. Following is my code:
routes.php(now)
Route::get('/about' , 'HomeController@gtn');
Route::get('test' , array('as' => 'test', 'uses' => 'HomeController@test'));
Controller(HomeController.php) has these functions:
public function test()
{
return 'Testing dude!!';
}
public function gets()
{
$data['stt'] = Mein::getstate();
return View::make('/about',$data);
}
public function gtn()
{
$data['shw'] = Mein::getCity();
return View::make('/about',$data);
}
My AJAX call(now):
$.ajax({
url:'{{route("about")}}',
type:'GET',
// data:'soni'
});
I want to access test
function of HomeController
. I checked the call from browser, it shows 404.
You don't call a controller directly (and definitely not like that!), but you call a url, or better a route (Laravel has named routes, it's good to use them):
$.ajax({
url:'{{route("about")}}', // or url: '{{url("about")}}'
type:'GET',
success: function(rxp){
// handle response here
// es. $('#mydiv').html(rxp);
}
});
Route::get('about' , array('as' => 'about', 'uses' => 'HomeController@gtn'));
Edit:
I want to access test function of HomeController
Then create a route for the test() method:
Route::get('test' , array('as' => 'test', 'uses' => 'HomeController@test'));
Update after comment:
I gave you blade syntax guessing you where using it in your template files. If you want to use blade, rename the file to filename.blade.php. Or else use php:
url:'<?php echo route("about");?>',
var redirect_url = 'about';
$.ajax({
type: 'GET',
url: 'about',
data: { 'facilityid' : y , 'status' : val }
}).done(function () {
window.location.href = redirect_url;
});
Route::get('about' , array('as' => 'about', 'uses' => 'HomeController@gtn'));