laravel:如何通过控制器中的 ajax 检索 POST 数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16119888/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 07:43:20  来源:igfitidea点击:

laravel: How to retrieve POST data through ajax in controller

jquerycontrollerlaravel

提问by iTenzo

I'm trying to retrieve $_POSTdata submitted through jquery ajax func but there seem to be nothing passed. Here is my code: first my form:

我正在尝试检索$_POST通过 jquery ajax func 提交的数据,但似乎没有任何通过。这是我的代码:首先是我的表格:

{{  Form::open('', 'POST', array('id'=>'ajax')) }}

    {{ Form::text('_date', '', array('id'=>'datepicker', 'class'=>'app_date')) }}
    {{ Form::submit('Go', array('id'=>'go')) }}

{{  Form::close() }}

And jquery:

和jQuery:

$('#ajax').submit(function(e){

    $.ajax({
        url: 'appajax',
        type: 'POST',
        data: $('.app_date').val(),
        dataType: 'json',
        success: function(info){
                     console.log(info);
         }
    });

    e.preventDefault();
});

... and finally in the controller:

...最后在控制器中:

public function post_appajax()
    {

        return Response::json(Input::get('_date'));

    }

And in console the output is NULL. What I'm I missing? I'm using laravel 3 currently downloadable from laravel.com. Thank you.

在控制台中,输出是NULL. 我错过了什么?我正在使用 laravel 3 目前可从 laravel.com 下载。谢谢你。

回答by undefined

As you are using POST method you should pass a key/value pair instead of a string, an object, change:

当您使用 POST 方法时,您应该传递一个键/值对而不是一个字符串,一个对象,更改:

data: $('.app_date').val(),

to:

到:

data: { _date: $('.app_date').val() },