Laravel passing data using ajax to controller

how to pass data from view to controller using ajax + laravel
laravel ajax post data to controller
how to get json data from controller using ajax in laravel
laravel ajax request to controller
laravel ajax url route with parameters
how to pass id in ajax url in laravel
get data from controller to view using ajax laravel
how to pass data from ajax to html

How do I pass the id from this ajax call to the TestController getAjax() function? When I do the call the url is testUrl?id=1

Route::get('testUrl', 'TestController@getAjax');

<script>
    $(function(){
       $('#button').click(function() {
            $.ajax({
                url: 'testUrl',
                type: 'GET',
                data: { id: 1 },
                success: function(response)
                {
                    $('#something').html(response);
                }
            });
       });
    });    
</script>

TestController.php

public function getAjax()
{
    $id = $_POST['id'];
    $test = new TestModel();
    $result = $test->getData($id);

    foreach($result as $row)
    {
        $html =
              '<tr>
                 <td>' . $row->name . '</td>' .
                 '<td>' . $row->address . '</td>' .
                 '<td>' . $row->age . '</td>' .
              '</tr>';
    }
    return $html;
}

In the end, I just added the parameter to the Route::get() and in the ajax url call too. I changed $_POST['id'] to $_GET['id'] in the getAjax() function and this got my response back

Route::get('testUrl/{id}', 'TestController@getAjax');

<script>
    $(function(){
       $('#button').click(function() {
            $.ajax({
                url: 'testUrl/{id}',
                type: 'GET',
                data: { id: 1 },
                success: function(response)
                {
                    $('#something').html(response);
                }
            });
       });
    });    
</script>

TestController.php

public function getAjax()
{
    $id = $_GET['id'];
    $test = new TestModel();
    $result = $test->getData($id);

    foreach($result as $row)
    {
        $html =
              '<tr>
                 <td>' . $row->name . '</td>' .
                 '<td>' . $row->address . '</td>' .
                 '<td>' . $row->age . '</td>' .
              '</tr>';
    }
    return $html;
}

Laravel passing data using ajax to controller, If you are developing rest apis better not to add token. If you are using 5.4 or 5.5 you can use api.php instead of web.php . In api.php you don't� Google about Laravel and Ajax and you will find it. You can always do it the normal way. Just call a route and let the controller for that route do the job. You can check if it is an ajax request on that route and respond.

Your ajax's method is GET but in controller you use $_POST to get value. This is problem.

You can you

$id = $_GET['id'];

But in Laravel, it have a pretty method to do this. It's here. You do not need to worry about the HTTP verb used for the request, as input is accessed in the same way for all verbs.

$id = Input::get("id");

If you want, you can filter request type to control exception. Docs here

Determine If The Request Is Using AJAX

if (Request::ajax())
{
    //
}

Laravel using ajax to pass data to controller and get response from , I am a begginer in laravel I want to create a blade where if I select a Country the div below list Cities of the Country (from database). I use Ajax, Route,� how to pass data from controller to view using ajax laravel 13th July 2020 Zubair Leave a comment I want to show a data controller to view using ajax and I have already shown a data controller to view on the chart bar without ajax I need to get data on the chart bar using ajax but I don’t have an idea how to show data on chart bar using ajax.

#in your controller function    
public function getAjax()
{
    #check if request is ajax
    if ($request->ajax()) {
        //your code
    }

    return $your_data;
}

Pass data from ajax to controller via post and back to blade, Forum > How to pass data in an Ajax call with routing through a controller As I noted in the code block, using Input::get('value') doesn't grab the value from the textbox (which makes I am assuming you are running laravel via the command pass data from ajax to controller in laravel Posted 4 years ago by massum i am having a problem in sending ajax get request to fetch the data in to the controller. what i am trying to achieve is the data i am sending through ajax i should be able to access those data and send it to another view. i am sharing my code below plz help javascript

How to pass data in an Ajax call with routing through a controller , But i need a little help with passing more data. how can i add more data or modify data in Duration: 25:58 Posted: Apr 21, 2019 You will learn how to write ajax request code in view file and how to pass data from view to controller using ajax in laravel 5.8. you can easily also send json response. If you are using post request then you must have to pass csrf token to ajax request. so in this example i will give you very simple example of ajax post request.

Laravel 5.8 Post Form Data From View to Controller Using Ajax , You can pass or send data to controller in laravel web applications: $.ajax({. url: ' http://localhost/blog/save-form' ,. type: "POST",. data: $('#contact_us').serialize(),. Maybe this is a very common topic, but i can't find any solution!, the app i'm developing is under Laravel 5.0, i need to send some data from jquery ajax to a laravel controller, i've followed this tutorial to post data using ajax, i've followed the steps and made the global configuration, so that i have a meta with the csrf token, when i send

How to pass data to a controller using Ajax in Laravel, Import jquery library in your view file to use ajax functions of jquery which will be used to send and receive data using ajax from the server. On the server side you can use the response() function to send response to client and to send response in JSON format you can chain the response function with json() function. You shouldn't use echo in the view. You shouldn't use ajax to get the view (unless you want to receive HTML and it does not seem the case here) You should have a different endpoint for your ajax request if you want to fetch the data asynchronously otherwise you can just return the view with all the data you need.

Comments
  • Instead of $id = $_GET['id']; i used Input::get("id") anyways it works thx alot
  • Don't I need to specify a route for the url in the $.ajax call?
  • $.ajax or $.post is the same in this case