Tuesday, April 4, 2017

GET data as response from POST

How could i possibly access response from php

$.ajax({
    dataType: 'json',
    type:'POST',
    url: form_action,
    data:{name:name, _token:token}
}).done(function(data){
    $("#add-modal").modal('hide');
    alert(data.id); //something i want to get from php
});

Here's part of my Laravel' controller

public function store(Request $request){
    $user = new User;
    $user->name = request('name'); //creates record with new mysql id 

    $user->save();
    return response()->json($user);
}

I want to acquire this mysql id that is generated while User is being created and parse it somehow to jquery. How could i possibly accomplish so?



via Ethris

Advertisement