LAravel 5.4:通过 Ajax 从控制器到 blade 的响应(laravel,json_encode()期望参数 2 为整数,给定对象)
我想将控制器中的数据作为数组响应到 AJAX,但它一直显示此错误:
json_encode() expects parameter 2 to be integer, object given
以下是我的代码:
function get_show(Request $request)
{
$id = $request->input('id');
$leads = Lead::find($id);
$leads_attendances = DB::table('event_attendances')
->leftJoin('leads', 'leads.id', '=', 'event_attendances.lead_id')
->leftJoin('groups', 'groups.id', '=', 'event_attendances.group_id')
->select('groups.name as event_name', 'leads.name as name', 'leads.company_name', 'event_attendances.attend as attend_status')
->where('event_attendances.lead_id', '=', $id)
->get()->toArray();
$output = array(
'id' => $id,
'country' => $leads->country,
'company_name' => $leads->company_name,
'lead_stage' => $leads->lead_stage
);
echo json_encode($output,$leads_attendances);
}
这是我的 AJAX 代码:
$.ajax({
url: "{{route('main.get_show')}}",
method: 'get',
data: {id: id},
dataType: 'json',
success: function (response) {
console.log(response);
$('#model2').modal('show');
document.getElementById('leads').innerHTML += response.output.company_name;
document.getElementById('leads').innerHTML += response.leads_attendances;
新错误:
leads:7515 Uncaught ReferenceError: leads_attendances is not defined at Object.success (leads:7515) at i (jquery.min.js:2) at Object.fireWith [as resolveWith] (jquery.min.js:2) at z (jquery.min.js:4) at XMLHttpRequest. (jquery.min.js:4)
incpect 的输出响应:
{"output":{"id":"25","country":"","company_name":"comp1","lead_stage":"under"},"leads_attendances":[{"event_name":"event1","name":"LEMUN","company_name":"comp1","attend_status":"Yes"},{"event_name":"event2","name":"ZDMJ","company_name":"comp1","attend_status":"Yes"},{"event_name":"event3","name":"MNAl","company_name":"comp1","attend_status":"Yes"},{"event_name":"event","name":"Khalai","company_name":"comp1","attend_status":"Yes"},{"event_name":"event4 ","name":"Zamel","company_name":"comp1","attend_status":"Yes"}]}
在 JS 中构建表格:
success: function (response) {
console.log(response);
$('#myModaleee').modal('show');
var leads_attendances=response.leads_attendances;
document.getElementById('lead_details').innerHTML += '<table>';
for(var i=0;i<leads_attendances.length;i++){
var event_name=leads_attendances[i].event_name;
var name=leads_attendances[i].name;
var company_name= leads_attendances[i].company_name;
var attend_status= leads_attendances[i].attend_status;
document.getElementById('lead_details').innerHTML += '<tr>'+
'<td>'+event_name+'</td>'+
'<td>'+name+'</td>'+
'<td>'+company_name+'</td>'+
'<td>'+attend_status+'</td>'
+'</tr>';
}
document.getElementById('lead_details').innerHTML += '</table>';
}
第一个数组 ($output) 已传递并与 Ajax 配合良好,但第二个集合显示该错误。我仍然需要传递除这些集合以外的更多内容,有什么帮助可以传递它并使其与 Ajax 配合使用吗?
谢谢
在您的控制器中返回如下所示的 Json 响应:
return response()->json($response,200);
function get_show(Request $request)
{
$id = $request->input('id');
$leads = Lead::find($id);
$leads_attendances = DB::table('event_attendances')
->leftJoin('leads', 'leads.id', '=', 'event_attendances.lead_id')
->leftJoin('groups', 'groups.id', '=', 'event_attendances.group_id')
->select('groups.name as event_name', 'leads.name as name', 'leads.company_name', 'event_attendances.attend as attend_status')
->where('event_attendances.lead_id', '=', $id)
->get()->toArray();
$output = array(
'id' => $id,
'country' => $leads->country,
'company_name' => $leads->company_name,
'lead_stage' => $leads->lead_stage
);
$response=array('output'=>$output,'leads_attendances'=>$leads_attendances);
return response()->json($response,200);
}
假设您有表 div:
<table>
<thead>
<tr>
<th> event_name </th>
<th> name </th>
<th> company_name </th>
<th> attend_status </th>
</tr>
</thead>
<tbody id="tbody"></tbody>
</table>
您的 ajax 函数应该是:
$.ajax({
url:'/url here',
method:'GET',
dataType:'json',
success:function(response){
console.log(response);
var id = response.output.id;
var country= response.output.country;
var company_name=response.output.company_name;
var lead_stage =response.output.lead_stage;
var leads_attendances=response.leads_attendances;
var trHTML="";
for(var i=0;i<leads_attendances.length;i++){
var event_name=leads_attendances[i].event_name;
var name=leads_attendances[i].name;
var company_name= leads_attendances[i].company_name;
var attend_status= leads_attendances[i].attend_status;
let trRow="<tr>"+
"<td>"+event_name+"</td>"+
"<td>"+name+"</td>"+
"<td>"+company_name+"</td>"+
"<td>"+attend_status+"</td>"
+"</tr>";
trHTML=trHTML+trRow;
}
$('#tbody').empty();
$('#tbody').append(trHTML);
},
error:function(err){
console.log(err);
}
});
您不能这样使用
echo json_encode($output,$leads_attendances);
试试这个
$data = ['output' => $output, 'leads_attendances' => $leads_attendances];
return response()->json(['results'=> $data]);
or
return response()->json($data);