简体   繁体   English

jQuery POST请求始终返回Laravel 4找不到的404

[英]jQuery POST request always returns 404 not found with Laravel 4

Every time I try to make a POST request with jQuery I keep receiving back the 404 error. 每当我尝试使用jQuery发出POST请求时,我都会不断收到404错误。

This is the UserController.php : 这是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 : 这是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. 如果我尝试导航到http://192.168.0.102/webname/public/signup ,则会加载页面并显示注册表单。

You're are using a "GET" type route. 您正在使用“ GET”类型的路由。

Let me explain. 让我解释。

If you want to use a route like /route/{something}/{different} you have to manualy generate an URL matching that route. 如果要使用/ route / {something} / {different}之类的路由 ,则必须手动生成与该路由匹配的URL。

URL::route('route', $something, $different)

Variable passed thought POST method are only available in the HTTP Headers. 通过POST方法传递的变量仅在HTTP标头中可用。

So you can't call Route::post('/route/{variable}') by passing variable though POST method. 因此,您无法通过POST方法传递变量来调用Route :: post('/ route / {variable}')。 Only with Route::get(). 仅与Route :: get()。

To get your variable with POST use 要使用POST获取变量,请使用

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. 但是尝试访问:/ webname / public / signup。

That pattern does not exist for POST, but just for GET. POST不存在该模式,而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. 我遇到了一些与此讨论相关的麻烦,我认为这些帖子不值得推荐:jQuery 帖子请求一直由Laravel get控制器处理。

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. Laravel正在将发布请求路由到get控制器。

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. 显然,Laravel将“ / test / x /”解释为GET路由,但将“ / test / x”解释为POST路由。 So the lesson is that Laravel routing can be subtle. 因此,教训是Laravel路由可能很微妙。 Hope that helps clarify the discussion a bit. 希望这有助于澄清讨论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM