0

I am trying to send some data on button click to a controller and display the response in a content div back in the view. However i am facing problem while sending the data to the controller. Here is what i am trying to do :

test.php

<script>
$(document).ready(function(){
    $("#btn1").click(function(){
        $.ajax({
            type: 'GET',
            url: 'get-response',
            dataType: 'json',
            data: {
                "id": "1"
            },
            success: function(response){

                $('#content').html(response.first);
            }
        });

    });

     $("#btn2").click(function(event){
        $.ajax({
            type: 'GET',
            url: 'get-response',
            dataType: 'json',
            data: {
                "id": "2"
            },
            success: function(response){

                $('#content').html(response.first);
            }
        });

    });
    });
    </script>


   <input type="button" id="btn1" value="Button1 Content" />
   <input type="button" id="btn2" value="Button2 Content" />
   <br>
   <div id="content"></div>

route.php

Route::get('ajax-example', function(){
return View::make('test');
});

Route::get('get-response/{id}', array('as'=>'get-response', 'uses'=>'AjaxController@getResult'));

AjaxController.php

public function getResult($id){
    if($id==1){
        $content = "Hello";

    }
    else if($id==2){
        $content = "World";

    }
    return Response::json(array('first'=>$content));
}

Error enter image description here Can anyone please help me out here. I am a bit confused right now. Thanks :)

2
  • 1
    Could you change this line url: 'get-response' to url: 'get-response/2' and then try? and remove data config property in ajax ? Commented Feb 9, 2015 at 6:32
  • Thanks for replying @Asik, but i guess i found the solution. I was not handling the url query string properly. So i just added $id = $_GET['id']; and that fixed the problem :) Commented Feb 9, 2015 at 6:48

2 Answers 2

1

if you need to get the parameter do this,

Input::get('id');

and route

Route::get('get-response', array('as'=>'get-response', 'uses'=>'AjaxController@getResult'));

because your ajax request is something like, host/get-response?id=5 and this is not compatible with Route::get('get-response/{id}'.., because this route needs something like host/get-response/5


Another way, you can keep your route as u declared and change the ajax request url,

   $.ajax({
        type: 'GET',
        url: 'get-response/'+1,
        dataType: 'json',
        data: {},
        success: function(response){

            $('#content').html(response.first);
        }
    });
Sign up to request clarification or add additional context in comments.

Comments

0

ok so i guess i figured it out what was causing the problem.

I changed my route to

Route::get('get-response', array('as'=>'get-response', 'uses'=>'AjaxController@getResult'));

and in controller i added

public function getResult(){
      $id = $_GET['id'];

Now it seems to working just fine :)

1 Comment

In your Laravel controller you should use $id = Input::get('id');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.