Reputation: 277
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
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
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