davidOhara
davidOhara

Reputation: 1008

Ajax Post to Controller Action

How to call an Action within a Controller in Laravel 4?! I tried it like this:

$.post('/tutorials/rate', {id:tutID, rating : rating});

in my controller i have this:

public function rate()  {
return whatever;
}

and in my routes.php:

Route::get('/tutorials/rate', array('uses' => 'TutorialController@rate'));

But it isn´t workin....

UPDATE: fixed it by myself...changed my controller function to:

public function post_rate() {
return whatever;
}

Upvotes: 1

Views: 769

Answers (2)

Tim
Tim

Reputation: 982

You are posting via Jquery, but then utilizing a Route::get method in Laravel.

Should be Route::post()

or

Route

Route::Controller('tutorials');

Controller Tutorials

public function postRate(){}

Upvotes: 1

Andreyco
Andreyco

Reputation: 22872

You use $.post() which means you are sending data using POST request.
You are not catching this method/url combination.

Modify your routes.php and you are good to go

Route::post('tutorials/rate', 'Tutorials@rate');

<?php

class Tutorials extends BaseController
{
    public function rate()
    {
        return 'rating';
    }
}

If I were you, I would use Resource (or maybe RESTfull) controllers.
But it depends on situation - good luck :)

Upvotes: 1

Related Questions