Ajax post to route in Laravel 4

10,235

Solution 1

In your route,

Route::get('data', array('uses' => 'HomeController@store'));

In HomeController,

public function store() {
  $input = Input::all(); // form data
  // validation rules
  $rules = array(
    'email'   => 'required|email', 
    'name'    => 'required', 
  ); 

  $validator = Validator::make($input, $rules); // validate
  // error handling
  if($validator->fails()) {
    if(Request::ajax()) {   // it's an ajax request                 
      $response = array(
         'response'  =>  'error',
         'errors'    =>  $validator->errors()->toArray()
      );                
    } else { // it's an http request
       return Redirect::intended('data')
                  ->withInput()
                  ->withErrors($validator);
    }
  } else { // validated
     // save data
  }
}

And finally the script,

var root_url = "<?php echo Request::root(); ?>/"; // put this in php file
$('#newPost :submit').click(function(e){
    var BASE = root_url + 'data';
    e.preventDefault();
    $.post(BASE, {
        'message' : $('#newPost textarea.message').val()
        }, function(data) {
        $('#content').prepend('<p>' + data + '</p>');
    });
});

Solution 2

You could change

var BASE = 'http://localhost/project/public/';

to

var BASE = '<php echo URL::route("name");?>'

Your route should then be:

Route::post('action', array('as' => 'name', 'uses' => 'HomeController@action'));

Note the use of the named routes instead of building the url using URL::to('controller/action')

Share:
10,235
Pars
Author by

Pars

Web and Mobile Developer at Chista Group. Prior lead Web Developer and Mobile Developer at Fanavari Tandis. Prior Web Developer at Lifeweb co. Prior Web Developer at Caspian co. Prior lead Web Developer at Padafand IT.

Updated on June 22, 2022

Comments

  • Pars
    Pars almost 2 years

    I am going to send some data to current page using ajax to insert something in database.
    Assume this ajax code:

    $('#newPost :submit').click(function(e){
                var BASE = 'http://localhost/project/public/';
        e.preventDefault();
        $.post(BASE, {
            'message' : $('#newPost textarea.message').val()
            }, function(data) {
            $('#content').prepend('<p>' + data + '</p>');
        });
    });
    

    This piece of code sends data to URL / and it works well. but I want to send it to a Route.Name which route sends it to a controller@action.
    is there anyway or workaround to do this?

  • Pars
    Pars over 10 years
    is this Laravel 4 syntax?
  • Pars
    Pars over 10 years
    so I need to use Request::ajax() to check whether it's an ajax request or not. in regular use, to send back errors I used $errors->has, how can I have access to $errors after ajax submiting?!
  • Pars
    Pars over 10 years
    so I need to use Request::ajax() to check whether it's an ajax request or not. in regular use, to send back errors I used $errors->has, how can I have access to $errors after ajax submiting?!
  • Damien Pirsy
    Damien Pirsy over 10 years
    You could send a different response in case you have errors or not