php - JSON post response is undefined/empty laravel 5 -
in head tag, declared this
<meta name="csrf-token" content="{{ csrf_token() }}" /> and script
$(document).ready(function(){ $.ajaxsetup({ headers: {'x-csrf-token': $('meta[name="csrf-token"]').attr('content') } }); $.post("/employees", {id : "1"}, function(response){ if(response.success) { var branchname = $('#branchname').empty(); $.each(response.employees, function(){ $('<option/>', { value:$(this).user_no, text:$(this).firstname }).appendto(branchname); }); } }, 'json'); //end of json post request }); //end of document ready and html select tag used when displaying json response
<select id="branchname"></select> my route use json post request
route::post('employees', [ 'as' => 'employees', 'uses' => 'mot@getemployee' ]); and controller used in route.
public function getemployee(){ $branch_no = "1"; $employees = mot_users::where("branch_no", $branch_no)->lists('firstname','lastname', 'user_no'); return response()->json(['success' => true, 'employees' => $employees]); } problem: yes did response seems data empty/undefined because select box empty yet occupied several option tags like
<select id="branchname"> <option></option> <option></option> <option></option> <option></option> <option></option> <option></option> <option></option> <option></option> <option></option> </select> and tried like
$.each(response.employees, function(){ alert($(this).user_no); //hoping id on json data response. }); instead of
$.each(response.employees, function(){ $('<option/>', { value:$(this).user_no, text:$(this).firstname }).appendto(branchname); }); but gives me "undefined".
questions: whats wrong code? missing something? why select tag id branchname empty? how can pass id post data key route controller querying stuff dont have manually set $branch_no variable instead use whats json post request sent routes controller?
ps: im newbie in laravel 5 i'm on part on figuring things out. help, suggestions, recommendations, ideas, clues fix issue appreciated. thank you.
Comments
Post a Comment