Ajax调用,显示json响应而不是成功?

时间:2017-07-26 12:57:43

标签: ajax laravel laravel-5

我在提交按钮上有一个带有click事件的表单,如下所示:

<div class="col-md-6 col-md-offset-4" id="submit_div">
    <button type="submit" id="submit" class="btn btn-warning" onclick="entity();">
        Update Entity
    </button>
</div>

AJAX:

$(document).ready(function() {
function entity() {
    $.ajax({
        url: 'entity/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+'">Edit</a></button')
        },
        error: function(data) {
            alert("Oh no!");
        },
        headers: {
            'X-CSRF-Token': $('meta[name="csrf-token"]').attr('content');
        }
    });
}
});

控制器:

public function store(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 response()->json(['results' => $data]);
}

路线:

Route::post('entity/create/add', 'EntityController@store');

一切似乎都有效,没有错误,但没有成功的按钮(数据),我得到了实际的回复:

{
    "results": 112
}

但我甚至不知道为什么会发生这种情况有人可以帮助我吗? form没有任何动作,看起来ajax没有被调用?

0 个答案:

没有答案