Laravel通过ajax发送表单,并使用请求进行验证?

时间:2017-08-08 12:55:20

标签: ajax laravel laravel-5

我正在做ajax,看起来像这样:

的Ajax:

$('form').submit(function(e) {
    e.preventDefault();
    var myform = document.getElementById('#createEntity');
    var fd = new FormData(myform);
    console.log($(this).serialize());
    $.ajax({
        url: "create/add",
        type: 'post',
        dataType: 'JSON',
        success: function(data) {
            $('#submit_div').html('<button type="button" class="btn.btn-success"><a href="/profile/entity/edit/'+ data.results +'">Edit</a></button');
        },
        error: function(data) {
            alert("Oh no!");
        },
        headers: {
        'X-CSRF-Token': $('meta[name="csrf-token"]').attr('content')
        }
    });
}

控制器:

public function storeEntity(EntityRequestCreate $request)
    {
        $geoloc = new Geoloc;
        $geoloc->lat = $request->input('lat');
        $geoloc->lng = $request->input('lng');
        $geoloc->slug = $request->input('name');
        $geoloc->save();

        $user_id = Auth::id();
        $entity = new Entity;
        $entity->name = $request->input('name');
        $entity->type = $request->input('type');
        $entity->email = $request->input('email');
        $entity->tags = $request->input('tags');
        $entity->_geoloc()->associate($geoloc);
        $entity->save();

        $entity_id = $entity->id;

        $address = new Address;
        $address->building_name = $request->input('building_name');
        $address->address = $request->input('address');
        $address->town = $request->input('town');
        $address->postcode = $request->input('postcode');
        $address->telephone = $request->input('telephone');
        $address->entity_id = $entity_id;
        $address->save();

        $role = User::find($user_id);
        $role->role = "2";
        $role->save();

        DB::table('entity_user')->insert(array('entity_id' =>  $entity_id, 'user_id' => $user_id));

        $result = $geoloc->save();
        $result2 = $entity->save();
        $result3 = $address->save();
        $result4 = $role->save();

        if($result && $result2 && $result3 && $result4) {
            $data = $entity_id;
        }else{
            $data = 'error';
        }
        return redirect('profile/entity');
    }

现在的问题是,我无法从表单中获取数据,它总是空的。通过ajax发送表单并在控制器中处理它的正确方法是什么?我已经尝试了上面这样的各种方法,onclick事件按钮,似乎没有任何工作,上面的代码提交和刷新页面由于某种原因,它从来没有成功

2 个答案:

答案 0 :(得分:3)

您没有向ajax请求添加任何数据。将data属性添加到请求对象。

$.ajax({
    url: "create/add",
    type: 'POST',
    dataType: 'JSON',
    data: $(this).serialize(),    // <-- Added data property
    success: function(data) {
        $('#submit_div').html('<button type="button" class="btn.btn-success"><a href="/profile/entity/edit/'+ data.results +'">Edit</a></button');
    },
    error: function(data) {
        alert("Oh no!");
    },
    headers: {
        'X-CSRF-Token': $('meta[name="csrf-token"]').attr('content')
    }
});

答案 1 :(得分:1)

根据jQuery.ajax函数&#39; s documentation,发送AJAX datax的正确语法是:

$.ajax({
  method: "POST",
  url: "some.php",
  data: { name: "John", location: "Boston" }
})

因此,请使用data: $(this).serialize(),和代码发布数据。