yii2 Ajax请求错误404

I want use ajax in yii2 (PHP framework) I use the following code but it does not work.

My view file (PHP):

<script>
var url1='<?php echo  Url::toRoute('Agehi/Ajaxshahr'); ?>';
</script>
<?php
$script= <<<JS
    function selectshahr()
    {
        var ost = $("#ostan").val();
        $.ajax(
        {
            type: "post",
            url: url1,
            data: ost,
            cache: false,
            success: function(data)
            {
                alert(data);
            }
        });

    }
JS;
$this->registerJs($script,View::POS_BEGIN);

$form = ActiveForm::begin([
    'id' => 'login-form',
    'options' => ['class' => 'form-horizontal','enctype'=>'multipart/form-data'],
]);
echo $form->errorSummary($model,['header'=>'لطفا خطاهای زیر را برطرف نمایید','class'=>'']);
echo \vova07\imperavi\Widget::widget([
    'selector' => '#content','name'=>'content',
    'settings' => [
        'lang' => 'fa',
        'minHeight' => 200,
        'plugins' => [
            'clips',
            'fullscreen'
        ]
    ]
]);
?>
<?= Html::label('استان','ostan',array()) ?>
<?= Html::dropDownList('ostan', null,
    ArrayHelper::map($ostan, 'id', 'name'),array('class'=>'form-control','onchange'=>'selectshahr()','id'=>'ostan')) ?>
<?= Html::label('شهرستان/شهر','shahr',array()) ?>
<?= Html::dropDownList('shahr', null,
    array(),array('class'=>'form-control')) ?>

and in my controller :

class AgehiController extends \yii\web\Controller
{
    public function  actionAjaxshahr($ostan)
    {
        $data = Shahr::findAll('condition', 'osid=' . $_POST['data']);
        if(yii::$app->request->isAjax())
        {
            return $this->renderPartial('_Ajax_shahr', array('data' => $model));
        }
        return $this->renderPartial('_Ajax_shahr', array('data' => $model));

    }

}

Everything seems okay but it does not respond to any request I checked with Browser developer tools and it saw AJAX as a 404 error

1个回答


路由中的控制器和动作名称应小写。</ p>
     </ div>

展开原文

原文

Controller and action names in a route should be lowercase.

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐