Every time I try to make a POST request with jQuery I keep receiving back the 404 error.
This is the UserController.php
:
class UserController extends BaseController {
public function signUp($username, $password, $email) {
$user = new User();
$user->setUsername($username);
$user->setPassword($password);
$user->setEmail($email);
return 'OK';
}
}
And this is the routes.php
:
Route::get('/signup', function(){
return View::make('signup');
});
Route::post('/signup/{username}/{password}/{email}', 'UserController@signUp');
I always receive this error:
POST http://192.168.0.102/webname/public/signup 404 (Not Found)
Why is that? If I try to navigate to http://192.168.0.102/webname/public/signup
the page is loaded and the signup form is shown.
You're are using a "GET" type route.
Let me explain.
If you want to use a route like /route/{something}/{different} you have to manualy generate an URL matching that route.
URL::route('route', $something, $different)
Variable passed thought POST method are only available in the HTTP Headers.
So you can't call Route::post('/route/{variable}') by passing variable though POST method. Only with Route::get().
To get your variable with POST use
Input::get('your_variable_name')
in your controller action.
Sorry for my bad english... A little bit tired, and I'm french too !
You are defining
Route::post('/signup/{username}/{password}/{email}', 'UserController@signUp');
But trying to access: /webname/public/signup.
That pattern does not exist for POST, but just for GET.
I had some troubles that were related to this discussion and in my opinion did not merit their own post: jQuery post requests kept getting handled by my Laravel get controller.
My routes:
Route::controller('/test','TestController');
In my view :
<script>
$(document).ready(function() {
jQuery.ajax({
type:'post',
url: '/test/x/',
success: function (response) {
alert(response);
}
});
});
</script>
My Controller:
public function getX() {
return 'Get';
}
public function postX() {
return 'Post';
}
On page load, I expected to see an alert reading "Post"... but instead I kept seeing "Get". Laravel was routing the post request to the get controller.
Solving this had to do with the trailing slash. Apparently, Laravel interpreted "/test/x/" as a GET route, but "/test/x" as a POST route. So the lesson is that Laravel routing can be subtle. Hope that helps clarify the discussion a bit.