實際上,我創建一個依賴下拉列表,但我的ajax js_base_url不會去控制器。我不知道有什麼問題。表名是system_detail和software_detail。和我的控制器的名字是SoftwaredetailController.php和SystemdetailController.php其實我使用depentdropdown列表與ajax,但它不工作
我的Ajax代碼:
function getEmployee(id){
$.ajax({
type:'POST',
url: JS_BASE_URL + 'systemdetail/get-employee',
data:{'id':id},
// dataType: 'text',
success: function(data) {
var options = '<option value="">Please Select</option>';
if (data) {
var json = JSON.parse(data);
for (var i = 0; i < json.length; i++) {
options +='<option value="'+json[i].system_id+'">'+json[i].system_id+'</option>'
}
$('#software_detail-system_id').html(options);
return false;
}
$('#software_detail-system_id').html('');
$('#software_detail-system_id').html(options);
},
});
}
我控制器代碼:
public function actionGetEmployee()
{
echo"<i am here>";die;
$postData = Yii::$app->request->post();
// echo "<pre>";
// print_r($postData['id']]);
// die;
$allSystemDetail = SystemDetail::find()
->where(['user_id' => $postData['id']])
->asArray()
->all();
if ($allSystemDetail){
return json_encode($allSystemDetail);
}else{
return false;
}
}
我的下拉列表列表代碼:
<?= $form->field($model, 'user_id')->dropDownList(
ArrayHelper::map(Employee::find()->asArray()->all(),'id','employee_id'),
[
'onchange' => 'getEmployee(this.value)',
'prompt'=>'Please Select',
])->label('Employee Id'); ?>
<?= $form->field($model, 'system_id')->dropDownList(
ArrayHelper::map(SystemDetail::find()->asArray()->all(),'system_id','system_id'),
[ 'prompt' => 'Select System id']); ?>
[依賴下拉列表yii2的可能重複。如何做?](https://stackoverflow.com/questions/23951520/dependent-dropdown-yii2-how-to-do) –
首先在谷歌搜索,你會得到很多解決方案。 https://github.com/kartik-v/yii2-widget-depdrop檢查這個鏈接。 –
我已經檢查谷歌和同樣的方法使用第二臺電腦它工作。但在這裏不工作,錯誤是「FORBIDDEN - 服務器瞭解請求,但拒絕履行它。」 (XHR):POST「 –