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

时间:2016-04-30 22:01:18

标签: php jquery json ajax laravel

对不起,我希望得到ajax post的聘用价值,

然后laravel controller获取数据(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

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

laravel controller

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

1 个答案:

答案 0 :(得分:0)

您需要先访问输入或使用$("#id").remove();上的get()方法:

$request