stef
3 years ago
126
11
Laravel

Laravel 5.2 jQuery ajax token 500 Error

Posted 3 years ago by stef

Hello @all,

i have got a little problen and don't find any solution at google. The example is from here: https://www.youtube.com/watch?v=WdZsN5QiQ_Q

This is my :

<meta name="csrf-token" content="{{ csrf_token() }}">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="//code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
<script type="text/javascript" src="{{ URL::to('js/custom.js') }}"></script>

My jQuery custom.js:

$("#formSearch input").on("click",function(e){
        $.ajaxSetup({
            headers: {
                'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
            }
        })
    e.preventDefault(); 
    var keywords = $(this).attr('id');
    if (keywords) {
        $.post("executeSearch",{keywords:keywords},function(markup){
                $("#counter").html(markup);
        });
    }
    else{
            alert(keywords);    }
 });

My routes:

Route::get('/',function(){
    return View::make("pages.index.index");
});
Route::post('executeSearch/',array("uses","[email protected]"));

And my Controller:

namespace App\Http\Controllers;

use DB;
use App\Http\Controllers\Controller;


class SearchController extends Controller{


    public function executeSearch(){ 

        $keywords = Input::get("keywords");

        $daten = Daten::all();
        $searchDaten = new \Illuminate\Database\Eloquent\Collection();

        foreach ($daten as $date) {
            if(Str::contains(Str::lower($date->name),Str::lower($keywords)))
        }
    $searchDaten->add($date);

        return View::make('searchedDaten')->with("searchDaten",$searchDaten);
    }

}

Thx :-)

Please sign in or create an account to participate in this conversation.