Post a form using ajax in Laravel

Namit

I know this question has been out there many times, however I am unable to get through this.

Here is my route:

Route::post('/masters/board/edit', 'MastersController@editBoard');

My Controller:

public function editBoard() {
        $board = Board::findOrFail(Input::get('id'));
        $board->nick_name        = Input::get('nick_name');
        $board->board_name        = Input::get('board');
        $board->type                        = Input::get('type');
        $board->save();

        return Redirect::action('MastersController@getBoards');
    }

My JS:

$("#edit_form").submit(function(e) {
    e.preventDefault();

    var type = "#edit_form";

    var formData = {
          id : $(type + " #id").val(),
          nick_name : $(type + " #nick_name").val(),
          name : $(type + " #board").val()
      }

    $.ajax({
      type: "POST",
      url: "masters/board/edit",
      data: formData,
      success: function(data) {
        console.log(data);
      }
    });
  });
});

This is throwing an error:

[Error] Failed to load resource: the server responded with a status of 500 (Internal Server Error) (edit, line 0)

Can anyone see a reason why?

chanafdo

Add a meta tag with the csrf_token.

<meta name="_token" content="{{ csrf_token() }}" />

And add that token to your data.

var formData = {
  id : $(type + " #id").val(),
  nick_name : $(type + " #nick_name").val(),
  name : $(type + " #board").val()
  _token: $('meta[name="_token"]').attr('content')
}

If you are going to perform lot of post requests try the following. Use the following script to setup your ajax requests with the csrf token.

$(function() {
  $.ajaxSetup({
    headers: {
      'X-CSRF-Token': $('meta[name="_token"]').attr('content')
    }
  });
});

If you are using laravel 5 this should work out of the box. If not you need to edit your app/filters.php file.

Replace the csrf filter with the following.

Route::filter('csrf', function() {
    $token = Request::ajax() ? Request::header('X-CSRF-Token') : Input::get('_token');
    if (Session::token() != $token)
        throw new Illuminate\Session\TokenMismatchException;
});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Post a form using ajax in Laravel

From Dev

How do I post a form in laravel 5 using ajax?

From Dev

Submit form laravel using AJAX

From Dev

Ajax post a form to controller (store) in Laravel

From Dev

Post multipart/form-data using Ajax

From Dev

PHP form passing post data using AJAX

From Dev

POST form on button click using jquery ajax

From Dev

Post request ajax with react using laravel in backend

From Dev

Updating MySQL Laravel 5.1 using Ajax Post

From Dev

Submit multiple form using ajax in laravel

From Dev

ajax post to laravel can't redirect,form submit can redirect

From Dev

AJAX Post for multiple form

From Dev

How to post a form to PHP using Ajax .serialize() data?

From Dev

Using Bootstrap validator.js with Jquery ajax form post

From Dev

Submitting Form using AJAX won't POST to database

From Dev

Return a value from a form POST using Razor and AJAX

From Dev

Post form data to Fw/1(Coldfusion) controller function using AJAX

From Dev

How to post a form to PHP using Ajax .serialize() data?

From Dev

Post form data onclick event of button using ajax in foreach loop

From Dev

MVC post form using Ajax on post back form state and JQuery reset

From Dev

Laravel MethodNotAllowedHttpException on POST form

From Dev

Laravel 4 Form Post

From Dev

Laravel form post to controller

From Dev

How to include ajax post into get form and exclude used inputs by ajax in get url? Laravel

From Dev

How to include ajax post into get form and exclude used inputs by ajax in get url? Laravel

From Dev

Laravel POST data from view to ROUTE without using a form

From Dev

Laravel 4 ajax POST

From Dev

Laravel 5 Ajax post

From Dev

ajax post security in laravel?

Related Related

HotTag

Archive