Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass data from ajax to laravel 5.2 controller via post method

Hello StackOverflow family. This is my very first question and I hope to get help.

I'm new to laravel framework and am using version 5.2 in my project. Am trying to pass data using the post method from my ajax function to a particular controller method but no data is passed to the controller.

I followed the steps in this forum https://laracasts.com/discuss/channels/laravel/process-data-in-controller-using-ajax-in-laravel but can't get it to work. Here is what I've done so far.

My JavaScript (post_script.js):

$.ajax({
    method: 'POST',
    url: './home',
    data: {
        userID: 76,
        userName: 'Jimmy'
     },
});

Note that this file is saved in assets/js directory in the laravel structure. Here is what I have in my route file (routes.php):

Route::get('/', "MyController@home");
Route::get('home', "MyController@home");

Here is the function I have in MyController.php file:

function home(Request $request) {
    $userID = $request['userID'];
    $userName = $request['userName'];
    return view('home', [
      'userID'=> $userID,
      'userName' => $userName
    ]);
}

In my view, I tried to access it like this:

<p>User ID: {{$userID}}</p>
<p>User Name: {{$username}}</p>

Nothing is displayed! Please what am I doing wrong? I need your help. Forgive me if my question is not proper but I hope you get what I mean. Thank you

like image 324
Fortune Avatar asked Mar 22 '16 19:03

Fortune


People also ask

Can ajax use POST?

post() makes Ajax requests using the HTTP POST method. The basic syntax of these methods can be given with: $. get(URL, data, success); —Or— $.

What is ajax POST method?

data : A plain object or string that is sent to the server with the request. success : A callback function that is executed if the request succeeds.it takes as an argument the returned data. It is also passed the text status of the response.


2 Answers

Your AJAX is POSTing, but you have no POST route set, only GET. Add a POST route, like so:

Route::post('home', "MyController@home");
like image 109
Don't Panic Avatar answered Sep 29 '22 22:09

Don't Panic


First check with your developer/network tool (eg. firebug) wether your ajax call reaches the desired controller/functions and that the parameters are forwarded correctly.

A safe way to specify Url in the ajax call in the Laravel environment is using the URL facade like this:

url: "{{ URL::to('home'); }}",

In order to do it like this however you must store your js as a myscript.blade.php (!!) file and @include it into your view accordingly.

For receiving your posted parameters in the controller function there is no need to declare function arguments, you can simply use the Input::Get() function eg. like this:

public function home()
{
  $userID = Input::Get('userID');
  $userName = Input::Get('userName');
  return view('home', [ 'userID'=> $userID, 'userName' => $userName ]);
}
like image 39
Malvolio Avatar answered Sep 29 '22 23:09

Malvolio