显示json响应

时间:2015-05-12 00:00:58

标签: ajax json laravel laravel-5

发送json post请求并获取返回数据时,我有这个json

backCell = newCell;

在我的控制器中,它获取id并查找记录/记录,其中$ branch_no等于$ id并获取列user_no,firstname和lastname并将其作为json响应返回。

 $.post("/employees", {id:"3"}, function(response){
    if(response.success)
    {
        var branchName = $('#branchname').empty();
        console.log(response.employees);
        $.each(response.employees, function(user_no, firstname, lastname){
            $('<option/>', {
                value:user_no,
                text: firstname + " " + lastname
            }).appendTo(branchName);
        });
    }
}, 'json'); 

现在它应该将响应显示为

public function getemployee(){
    $id = $_POST['id'];
    $employees = mot_users::where("branch_no", $id)
    ->select(array('user_no', 'lastname', 'firstname'))
    ->get()->toArray();
    return response()->json(['success' => true, 'employees' => $employees]);
}

但显示为

<select>
    <option value="1">Firstname Lastname</option>
    <option value="2">Firstname Lastname</option>
    <option value="3">Firstname Lastname</option>
    <option value="4">Firstname Lastname</option>
</select>

我的控制台中没有错误,我认为我错误地获取了json响应所以任何想法,帮助,线索,建议和推荐使这项工作成功?

1 个答案:

答案 0 :(得分:0)

试试这个

$.post("/employees", {id:"3"}, function(response){
    if(response.success)
    {
        var branchName = $('#branchname').empty();
        console.log(response.employees);
        $.each(response.employees, function(index, value){
            $('<option/>', {
                value:user_no,
                text: value.firstname + " " + value.lastname
            }).appendTo(branchName);
        });
    }
}, 'json');