dkapa
dkapa

Reputation: 277

Receive data with jQuery POST ajax request and Laravel 4

I'm trying to use an old Laravel 3 code in my new Laravel 4 app.

When I send a ajax post request I can't receive the data.

View:

   request = $.ajax({
        url: "/clients/jsonbyname/",
        type: "POST",
        data: {
                "name": "hi"
              }
    });

   request.done(function (res, textStatus, jqXHR){
        if (res.status = "ok"){     
        console.log(res);
       }
   }

Route:

Route::get('/clients/jsonbyname', 'Clients@postJsonbyname');

Changed to: (update)

Route::post('/clients/jsonbyname', 'Clients@postJsonbyname');

Controller:

public function postJsonbyname(){
    return dd(Input::get('name'));
}

Old: I cannot received the "name" data and I received NULL. Update: Console give me 404 error

This was working in Laravel 3 and I don't know what's wrong.

Thank you

Upvotes: 0

Views: 7741

Answers (3)

TunaMaxx
TunaMaxx

Reputation: 1769

You are sending a POST request to a GET route.

Upvotes: 0

Musa
Musa

Reputation: 97672

You're making a 'POST' request in your ajax call, but you're expecting a GET request. Use a GET request.

   request = $.ajax({
        url: "/clients/jsonbyname/",
        type: "GET",
        data: {
                "name": "hi"
              }
    });

   request.done(function (res, textStatus, jqXHR){
        if (res.status = "ok"){     
        console.log(res);
       }
   }

Upvotes: 1

Jeff Lambert
Jeff Lambert

Reputation: 24661

Your route says its get, but you're posting it. Change it to Route::post(...) or change your request from the client side to get:

type: "GET",

Upvotes: 1

Related Questions