当我选择下拉列表(customerid)时,我正在尝试自动填充文本框(Customername,CustomerAddress,customerMobileno)。成功选择了客户ID,但在获取控制器操作时出现问题。我收到了404错误。我想提一下我在这里使用的是另一个 - Yii2. Access to higher level folder。
在萤火虫中我收到以下错误 -
<?= $form->field($model, 'o_customerid')->widget(Select2::classname(), [
'data' => ArrayHelper::map(Customer::find()->all(),'c_id','customerDetails'),
'language' => 'en',
'options' => ['placeholder' => 'Select Customer Details', 'id' => 'custid'],
'pluginOptions' => [
'allowClear' => true
],
]);
?>
<?php
$script = <<< JS
$('#custid').change(function(){
var custid = $(this).val();
$.get('index.php?r=orders/customer/get-for-customer',{ custid : custid }, function(data){
alert(data);
var data = $.parseJSON(data);
$('#orders-o_customername').attr('value',data.c_name);
$('#orders-o_customeraddress').attr('value',data.c_address);
$('#orders-o_customermobno').attr('value',data.c_mobileno);
});
});
JS;
$this->registerJs($script);
?>
我的select2小部件代码和相关的javascript -
public function actionGetForCustomer($custid)
{
$customer = Customer::find()->where(['c_id'=>$custid])->asArray()->one();
echo Json::encode($customer);
}
CustomerController中的控制器操作 -
{{1}}
答案 0 :(得分:1)
如果你使用漂亮的网址,你的结果网址应该是
://localhost:8080/amitopticals/orders/customer/get-for-customer?custid=2
而不是生成的邮件标题
://localhost:8080/amitopticals/orders/orders/....php?r=orders/customer/get-for-customer&custid=2
所以你为$ get而不是
$.get('index.php?r=orders/customer/get-for-customer',{ custid : custid }, function(data){
alert(data);
........
你应该使用
$.get('customer/get-for-customer',{ custid : custid }, function(data){
alert(data);