JQueryajax将数据(json)发布到laravel,然后laravel创建数据


JQuery ajax post data(json) to laravel, then laravel create data

对不起,我想获得ajax帖子的使用价值,

然后laravel控制器将数据(json)插入数据库,

但是jquery.map"bruce"是错误的,我该怎么办,请帮帮我,谢谢!

这是json格式化

{"data":{"bruce":"[{"employ":"bruce"},{"employ":"peter"}]","_token":"UiKUMMZRqTgYv5"}}

HTML

<input type="button" class="btn ahr-button_2 employ" value="bruce">
<input type="button" class="btn ahr-button_2 employ" value="peter">
<input type="button" class="btn ahr-button_2 employ" value="abcd">
<input type="button" class="btn ahr-button_2 employ" value="efgh">
<a href="#" class="finish_sumbit">完了</a>

Javascript

$('.employ').click(function(){
    $(this).toggleClass('active');
});
$(".finish_sumbit").click(function(e){
 e.preventDefault();
  var data = $('.employ.active').map(function() {
    return {
      'employ': this.value
    }
  }).get();
$.ajax({
    type: "POST",
    url: "business_a",
    async:false,
    dataType: "json",
    data: {bruce:data,_token:token},
    success: function (data) {
        console.log(data);
    },
    error: function (data) {
        console.log('Error:', data);
    }
});
});

laravel路由

Route::post('/business_a', 'BusinessController@business_a');

laravel控制器

public function business_a(Request $request)
    {
        $employ = new Employ;
        $b = $employ::create([
                'employ' => $request->bruce,          
        ]);
     }

您需要首先访问输入或在$request:上使用get()方法

public function business_a(Request $request)
{
    $employ = new Employ;
    $b = $employ::create([
            'employ' => $request->get('employ')        
    ]);
 }