7

I need to send data via JS to a Laravel controller on a button click. I'm not using any form because the data is being created dynamically. Every time i try to send the data, i get an Internal Server Error (500), but unable to catch that exception in the controller or the laravel.log file.

Here's what i'm doing:

Route:

Route::post('section/saveContactItems', 'SectionController@saveContactItems');

Controller:

public function saveContactItems($id, $type, $items, $languageID = "PT"){ ... }

JS:

$('button').on("click", function (evt) {

    evt.preventDefault();

    var items = [];
    var id = $("#id").val();
    var languageID = $("#languageID").val();
    var data = { id: id, type: type, items: JSON.stringify(items), languageID: languageID };

    $.ajax({
        url: "/section/saveContactItems",
        type: "POST",
        data: data,
        cache: false,
        contentType: 'application/json; charset=utf-8',
        processData: false,
        success: function (response)
        {
            console.log(response);
        }
    });
});

What am i doing wrong? How can i accomplish this?

UPDATE: Thanks to @ShaktiPhartiyal's answer (and @Sanchit's help) i was able to solve my issue. In case some else comes into a similar problem, after following @Shakti's answer i wasn't able to access the data in the controller. So, i had to stringify the data before sending it to the server:

data: JSON.stringify(data),
3
  • When you are using POST, you have to pass csrf_token field ... Commented Apr 4, 2017 at 17:57
  • @sanchitGupta i've tried that to, but was unsuccessful too... Commented Apr 4, 2017 at 17:58
  • 1
    Okay... Lets try with @ShaktiPhartiyal answer below.... and let me know if its work Commented Apr 4, 2017 at 18:00

1 Answer 1

8

You do not need to use

public function saveContactItems($id, $type, $items, $languageID = "PT"){ ... }

You have to do the following:

public function saveContactItems()
{
    $id = Input::get('id');
    $type = Input::get('type');
    $items = Input::get('items');
    $languageID = Input::get('languageID');
}

and yes as @Sanchit Gupta suggested you need to send the CSRF token with the request:

Methods to send CSRF token in AJAX:

$.ajaxSetup({
    headers: {
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
    }
});

If you use this approach you need to set a meta tag like so:

<meta name="csrf-token" content="{{csrf_token()}}">

or

data: {
        "_token": "{{ csrf_token() }}",
        "id": id
        }

UPDATE

as @Sanchit Gupta pointed out use the Input facade like so:

use Input;
Sign up to request clarification or add additional context in comments.

5 Comments

I can finally access the controller action :) Now i get the following error: exception 'Symfony\Component\Debug\Exception\FatalErrorException' with message 'Class 'App\Http\Controllers\Input' not found'. This is caused by $id = Input::get('id');
Use Input facade at top of your controller
Solved the error, but the data comes up is empty. This is what i'm sending to the controller: Object {id: "5", type: "email", items: "[]", languageID: "PT"}
Its working! just needed to stringify the data before sending it to the controller: JSON.stringify(data);
Well Explained @ShaktiPhartiyal +1. Happy Coding :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.