php - read JSON data sent using post in jquery with laravel framework -
i have codes,
var obj = '{"items":[{"code":"c101","description":"car"}]}'; $.post('get-items',obj,function(){ });
i used code,
file_get_contents('php://input')
because cant post data sent. using above code, raw post data.
how can read data sent without using file_get_contents('php://input')? because can't use file_get_contents('php://input').
here laravel controller function,
public function getitems() { $data = file_get_contents('php://input'); if(isset($data)) { ... } }
laravel 5.3 expects input sent in array format https://laravel.com/docs/5.3/requests#retrieving-input
request sent through jquery
$.ajax({ url: 'http://weburl.com/api/user/create', datatype: 'json', type: 'post', data: {'user': user}, success: function(data) { this.setstate({data: data}); }.bind(this), error: function(xhr, status, err) { console.error(null, status, err.tostring()); }.bind(this) });
laravel usercontroller::create
public function create(request $request) { $user = new user(); $user->name = $request->input('user.name'); $user->email = $request->input('user.email'); $user->password = $request->input('user.password'); $user->save(); return response($user, 201); }
Comments
Post a Comment