Having problems submitting form data using ajax to controller action (laravel)

Route:

Route::post('admin/cms/create','CmsController@createmenu');

Controller action:

public function createmenu(Request $request){
        $menu = new menu;
        $this->validate($request,[
            'name'=>'required',
            's_title'=>'required_if:s_exist,1',
            's_desc'=>'required_if:s_exist,1',
            's_path'=>'required_if:s_exist,1',
            'category'=>'required'
        ]);
        $path=$request->file('s_path')->store('img/slideshow');
        $menu::create([
            'name'=>$request->name,
            's_exist'=>$request->s_exist,
            's_title'=>$request->s_title,
            's_desc'=>$request->s_desc,
            's_path'=>$path,
            'category'=>$request->category
        ]);
        return redirect('admin/cms');
    }

Ajax (jquery):

$("#f_ins_menu").on("submit",function(e){
    e.preventDefault();
    var data={};
    $.ajax({
        type:"POST",
        //url:$(this).attr("action"), //I would like it to work with this dynamic url
        url:"cms/create", //this file is inside the folder admin
        //I also tried without the map function
        data:$(this).serializeArray().map(function(x){data[x.name] = x.value;}),
        contentType:false,
        cache:false,
        processData:false,
    });
});

Createmenu action works without ajax

I have another action that uses Ajax and it works, but Request $requestI don’t pass the difference in this action , in fact I don’t miss anything.

I tried in native PHP and it works. Note that I added contentType:false, cache:false, processData:false,, because I also transfer the file.

I tried to die and dumb (dd) the $ request parameter, and I get a huge block of code, I think the class is, so my thoughts are that the Request $ request parameter does not receive the data that I pass through ajax

And yes, I enabled CSRF_FIELD with the meta tag trick I found here https://laravel.com/docs/5.4/csrf

- , , , , json, .

, , , . /.

public function createmenu(Request $request){
    return dd($request); //I've tried this but it returns alot of code in console.log when I do the success:function()
}

, , , .

EDIT:

, HTTP- 422 HTTP- 500

2:

, die dumb (dd()) ajax, return . ajax, , @Lorav, :

data:{data:$(this).serialize()},

$data = $request->data;

Request, .

+4
1

$request- > all(), .

+1

Source: https://habr.com/ru/post/1673623/


All Articles