Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

laravel: How to retrieve POST data through ajax in controller

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

{{  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:

$('#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.

like image 611
iTenzo Avatar asked Feb 17 '23 18:02

iTenzo


1 Answers

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

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

to:

data: { _date: $('.app_date').val() },
like image 142
undefined Avatar answered Feb 26 '23 21:02

undefined