1. <small id='sW6zr'></small><noframes id='sW6zr'>

      <i id='sW6zr'><tr id='sW6zr'><dt id='sW6zr'><q id='sW6zr'><span id='sW6zr'><b id='sW6zr'><form id='sW6zr'><ins id='sW6zr'></ins><ul id='sW6zr'></ul><sub id='sW6zr'></sub></form><legend id='sW6zr'></legend><bdo id='sW6zr'><pre id='sW6zr'><center id='sW6zr'></center></pre></bdo></b><th id='sW6zr'></th></span></q></dt></tr></i><div id='sW6zr'><tfoot id='sW6zr'></tfoot><dl id='sW6zr'><fieldset id='sW6zr'></fieldset></dl></div>

      <tfoot id='sW6zr'></tfoot>

        <legend id='sW6zr'><style id='sW6zr'><dir id='sW6zr'><q id='sW6zr'></q></dir></style></legend>

          <bdo id='sW6zr'></bdo><ul id='sW6zr'></ul>

        如何在yii2中进行Json编码?

        how to Json encode in yii2?(如何在yii2中进行Json编码?)

            <small id='JfYsD'></small><noframes id='JfYsD'>

              <tbody id='JfYsD'></tbody>
              • <tfoot id='JfYsD'></tfoot>
                <legend id='JfYsD'><style id='JfYsD'><dir id='JfYsD'><q id='JfYsD'></q></dir></style></legend>
                <i id='JfYsD'><tr id='JfYsD'><dt id='JfYsD'><q id='JfYsD'><span id='JfYsD'><b id='JfYsD'><form id='JfYsD'><ins id='JfYsD'></ins><ul id='JfYsD'></ul><sub id='JfYsD'></sub></form><legend id='JfYsD'></legend><bdo id='JfYsD'><pre id='JfYsD'><center id='JfYsD'></center></pre></bdo></b><th id='JfYsD'></th></span></q></dt></tr></i><div id='JfYsD'><tfoot id='JfYsD'></tfoot><dl id='JfYsD'><fieldset id='JfYsD'></fieldset></dl></div>
                  <bdo id='JfYsD'></bdo><ul id='JfYsD'></ul>
                • 本文介绍了如何在yii2中进行Json编码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  尝试对 json 进行编码并在 yii2 中接收 400: Bad Request.我正在尝试在 Rest 客户端中编码,但无法正常工作.

                  Attempting to encode json and receive 400: Bad Request in yii2. I am trying to encode in Rest client but it is not working properly.

                  <?php 
                      namespace appcontrollers;
                      use Yii;
                      use yiifiltersAccessControl;
                      use yiiwebController;
                      use yiifiltersVerbFilter;
                      use appmodelsTblUserRegistration;
                      class UserController extends Controller
                      {
                          public function actionRegister()
                          {
                              $model = new TblUserRegistration();
                              $username = $_POST['username'];
                              echo json_encode($username);
                          }
                      }
                  ?>
                  

                  错误图片.

                  错误图片

                  推荐答案

                  解决方案 1: 如果您的控制器的所有操作都将传递 json,您也可以考虑扩展 yii estController 而不是 yiiwebController :

                  Solution 1: In case if all your controller's actions will deliver json you may also consider extanding yii estController instead of yiiwebController :

                  namespace appcontrollers;
                  
                  use Yii;
                  
                  class UserController extends yii
                  estController
                  {
                      public function actionRegister()
                      {
                          $username = Yii::$app->request->post('username');
                          return $username;
                      }
                  }
                  

                  注意:您也可以使用 ActiveController它扩展了 yii estController(参见 rest docs) 如果你需要处理 CRUD 操作.

                  NOTE: you may also use ActiveController which extends yii estController (see rest docs) if you need to handle CRUD operations.

                  解决方案 2: 扩展 yiiwebController 是通过使用 yiifilters内容协商器.请注意,此处将 $enableCsrfValidation 设置为 false 可能是强制性的,因为它的 相关文档 :

                  Solution 2: A different approach when extending yiiwebController is by using yiifiltersContentNegotiator. Note that setting $enableCsrfValidation to false may be mandatory here as it is explained in its related docs :

                  是否启用 CSRF(跨站点请求伪造)验证.默认为真.当启用 CSRF 验证时,表单提交到Yii Web 应用程序必须源自同一个应用程序.否则,将引发 400 HTTP 异常.

                  Whether to enable CSRF (Cross-Site Request Forgery) validation. Defaults to true. When CSRF validation is enabled, forms submitted to an Yii Web application must be originated from the same application. If not, a 400 HTTP exception will be raised.

                  注意,此功能需要用户客户端接受cookie.还,要使用此功能,通过 POST 方法提交的表单必须包含一个名称由 $csrfParam 指定的隐藏输入.您可以使用yiihelpersHtml::beginForm() 生成他的隐藏输入.

                  Note, this feature requires that the user client accepts cookie. Also, to use this feature, forms submitted via POST method must contain a hidden input whose name is specified by $csrfParam. You may use yiihelpersHtml::beginForm() to generate his hidden input.

                  上面的代码可以这样改写:

                  The above code may be rewritten this way :

                  namespace appcontrollers;
                  
                  use Yii;
                  use yiiwebController;
                  use yiifiltersContentNegotiator;
                  use yiiwebResponse;
                  
                  class UserController extends Controller
                  {
                      public $enableCsrfValidation = false;
                  
                      public function behaviors()
                      {
                          return [
                              'contentNegotiator' => [
                                  'class' => ContentNegotiator::className(),
                                  'formats' => [
                                      'application/json' => Response::FORMAT_JSON,
                                  ],
                                  'only' => ['register'],
                              ],
                          ];
                      }
                  
                      public function actionRegister()
                      {
                          $username = Yii::$app->request->post('username');
                          return $username;
                      }
                  }
                  

                  这篇关于如何在yii2中进行Json编码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
                  PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
                  mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
                  Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
                  Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
                  Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)
                  <legend id='OrxR0'><style id='OrxR0'><dir id='OrxR0'><q id='OrxR0'></q></dir></style></legend>

                  <tfoot id='OrxR0'></tfoot>
                  1. <small id='OrxR0'></small><noframes id='OrxR0'>

                      <tbody id='OrxR0'></tbody>

                          <bdo id='OrxR0'></bdo><ul id='OrxR0'></ul>

                          <i id='OrxR0'><tr id='OrxR0'><dt id='OrxR0'><q id='OrxR0'><span id='OrxR0'><b id='OrxR0'><form id='OrxR0'><ins id='OrxR0'></ins><ul id='OrxR0'></ul><sub id='OrxR0'></sub></form><legend id='OrxR0'></legend><bdo id='OrxR0'><pre id='OrxR0'><center id='OrxR0'></center></pre></bdo></b><th id='OrxR0'></th></span></q></dt></tr></i><div id='OrxR0'><tfoot id='OrxR0'></tfoot><dl id='OrxR0'><fieldset id='OrxR0'></fieldset></dl></div>