服务端数据 return json_encode(['status' => 200, 'data' => $data]);
页面js $("#pid").on('change', function () { var pid = $(this).val(); //获取下拉列表选中的值 $.ajax({ type: 'post', url: 'linkage', data: {key: pid}, dataType: 'json', success: function (data) { console.log(data); var status = data.status; var pid = data.data; if (status == 200) { //判断状态码,200为成功 var option = ''; option += '<option value="0">全部</option>'; for (var i = 0; i < pid.length; i++) { option += '<option value="' + pid[i]['news_category_id'] + '">' + pid[i]['news_category_name'] + '</option>'; } } else { var option = '<option>请选择二级栏目</option>'; //默认值 } if (pid.length == 0) { var option = '<option>请选择一级栏目</option>'; //默认值 } $("#id").html(option); //js刷新第二个下拉框的值 }, }); });