• <bdo id='rVESR'></bdo><ul id='rVESR'></ul>

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

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

        在 Yii2 中获取 JSON 格式的响应

        Get response in JSON format in Yii2(在 Yii2 中获取 JSON 格式的响应)
          <tfoot id='Wfjsf'></tfoot>

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

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

                  本文介绍了在 Yii2 中获取 JSON 格式的响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试将响应数组转换为 JSON 格式.我已经尝试了在 SO 和其他网站上发布的所有答案,例如 web1,web2 添加 header('内容类型:application/json') 然后 echo json_encode($data,JSON_PRETTY_PRINT);但我总是以文本格式获得输出.有人能帮我解决这个问题吗.

                  I'm trying to convert and array of response into JSON format. I have tried all the answers that were posted on SO and other websites like web1,web2 adding header('Content-Type: application/json') and then echo json_encode($data,JSON_PRETTY_PRINT); But I'm always getting the output in text format. Can some one help me in solving this.

                  助手类:

                  public static function renderJSON($data) {
                      header('Content-Type: application/json');
                      echo json_encode($data,JSON_PRETTY_PRINT);
                  }
                  

                  我的控制器:

                  if ($model->login()) {
                      $user =  User::findByUsernameOrEmail($request->post('username'));
                      $userArray = ArrayHelper::toArray($user);
                      Helpers::renderJSON($userArray);
                  

                  我尝试打印 userArray,它看起来像这样:

                  I tried to printing the userArray and it looks like this:

                  Array
                  (
                      [name] => abc
                      [lastname] => xyz
                      [username] => test_test
                  )
                  

                  Json 输出:(html/text)

                  Json output: (html/text)

                  {
                      "name": "abc",
                      "lastname": "xyz",
                      "username": "test_test"
                  }
                  

                  推荐答案

                  设置

                  Yii::$app->response->format = yiiwebResponse::FORMAT_JSON;
                  

                  return之前的某个控制器的动作中.

                  in the controller's action somewhere before return.

                  这篇关于在 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 的问题)
                  <i id='aZHnb'><tr id='aZHnb'><dt id='aZHnb'><q id='aZHnb'><span id='aZHnb'><b id='aZHnb'><form id='aZHnb'><ins id='aZHnb'></ins><ul id='aZHnb'></ul><sub id='aZHnb'></sub></form><legend id='aZHnb'></legend><bdo id='aZHnb'><pre id='aZHnb'><center id='aZHnb'></center></pre></bdo></b><th id='aZHnb'></th></span></q></dt></tr></i><div id='aZHnb'><tfoot id='aZHnb'></tfoot><dl id='aZHnb'><fieldset id='aZHnb'></fieldset></dl></div>
                  • <legend id='aZHnb'><style id='aZHnb'><dir id='aZHnb'><q id='aZHnb'></q></dir></style></legend>

                      • <bdo id='aZHnb'></bdo><ul id='aZHnb'></ul>
                          <tbody id='aZHnb'></tbody>

                          <tfoot id='aZHnb'></tfoot>

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