Javascript Yii2 中的 Ajax + 控制器动作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28831860/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 02:29:04  来源:igfitidea点击:

Ajax + Controller Action in Yii2

javascriptphpjqueryajaxyii2

提问by user3640056

I'm new to programming, and I'm trying to call a function when the user inputs data and clicks submit button. I'm using Yii2 and I'm not familiar with Ajax. I tried developing a function, but my controller action isn't called.

我是编程新手,当用户输入数据并单击提交按钮时,我试图调用一个函数。我正在使用 Yii2 并且我不熟悉 Ajax。我尝试开发一个函数,但没有调用我的控制器操作。

Here is the example code I'm trying:

这是我正在尝试的示例代码:

views/index.php:

意见/index.php:

<script>
    function myFunction()
    {
        $.ajax({
            url: '<?php echo Yii::$app->request->baseUrl. '/supermarkets/sample' ?>',
           type: 'post',
           data: {searchname: $("#searchname").val() , searchby:$("#searchby").val()},
           success: function (data) {
              alert(data);

           }

      });
    }
</script>

<?php
use yii\helpers\Html;
use yii\widgets\LinkPager;

?>
<h1>Supermarkets</h1>
<ul>

<select id="searchby">
    <option value="" disabled="disabled" selected="selected">Search by</option>
    <option value="Name">Name</option>
    <option value="Location">Location</option>
</select>

<input type="text" value ="" name="searchname", id="searchname">
<button onclick="myFunction()">Search</button>
<h3> </h3>

Controller:

控制器:

public function actionSample(){         
     echo "ok";
}

My problem is that when I click on the Search button nothing happens, and when I try to debug it, the debugger runs no code!

我的问题是,当我单击“搜索”按钮时什么也没有发生,当我尝试调试它时,调试器不运行任何代码!

回答by ankitr

This is sample you can modify according your need

这是您可以根据需要修改的示例

public function actionSample()
{
if (Yii::$app->request->isAjax) {
    $data = Yii::$app->request->post();
    $searchname= explode(":", $data['searchname']);
    $searchby= explode(":", $data['searchby']);
    $searchname= $searchname[0];
    $searchby= $searchby[0];
    $search = // your logic;
    \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
    return [
        'search' => $search,
        'code' => 100,
    ];
  }
}

If this will success you will get data in Ajax success block. See browser console.

如果这会成功,您将在 Ajax 成功块中获得数据。请参阅浏览器控制台。

  $.ajax({
       url: '<?php echo Yii::$app->request->baseUrl. '/supermarkets/sample' ?>',
       type: 'post',
       data: {
                 searchname: $("#searchname").val() , 
                 searchby:$("#searchby").val() , 
                 _csrf : '<?=Yii::$app->request->getCsrfToken()?>'
             },
       success: function (data) {
          console.log(data.search);
       }
  });

回答by Abdallah Awwad Alkhwaldah

you have to pass _csrf tokin as a parameter

您必须将 _csrf tokin 作为参数传递

_csrf: yii.getCsrfToken()

or you can disable csrf valdation

或者您可以禁用 csrf 验证

回答by Danil Pervozdanniy

The correct way to get the CSRF param is this:

获取 CSRF 参数的正确方法是:

data[yii.getCsrfParam()] = yii.getCsrfToken()