【问题标题】:CakePhp 3 : Ajax - return array with html blockCakePhp 3:Ajax - 返回带有 html 块的数组
【发布时间】:2015-11-24 23:00:04
【问题描述】:

这个 ajax 请求将两个参数 x 和 y 发送到 php 控制器:

           $.ajax({
              url : 'feuilles/test',
              data: {
                      x: x, y: y 
                  },
              type : 'post',
              dataType : 'json',
                success : function(res){
                   $.each(res, function(index, element) {
                      alert("index "+index + " element "+element);
                    });
                },

               error : function(result, statut, erreur){
                console.log(result);                   
               },

               complete : function(resultat, statut){
                 //"completed"
               }
           });

这是 php 控制器:

public function test(){
    $x = $this->request->data['x'];
    $y = $this->request->data['y'];
    $actualFeuille = $this->request->session()->read('actualFeuille');
    $res = $actualFeuille->getListOfPossibilies(0,2);
    $this->set('res', $res);
}

$res 得到一个数组,这是它的内容:

[
(int) 0 => (int) 1,
(int) 1 => (int) 2,
(int) 2 => (int) 3,
(int) 3 => (int) 4,
(int) 4 => (int) 5,
(int) 5 => (int) 6,
(int) 6 => (int) 7,
(int) 7 => (int) 8,
(int) 8 => (int) 9,
(int) 9 => (int) 10,
(int) 10 => (int) 11,
(int) 11 => (int) 12,
(int) 12 => (int) 13,
(int) 13 => (int) 14
]

当我返回响应时,它显示存在错误,事实是响应 html 内容中收到的正文就像以下示例一样:

"<div class="cake-debug-output"> <span><strong>\src\Model\Entity\Feuille.php</strong> (line <strong>68</strong>)</span> <pre class="cake-debug"> (int) 15 </pre> </div><div class="cake-debug-output"> <span><strong>\src\Model\Entity\Feuille.php</strong> (line <strong>61</strong>)</span> <pre class="cake-debug"> [ (int) 0 =&gt; (int) 1, (int) 1 =&gt; (int) 2, (int) 2 =&gt; (int) 3, (int) 3 =&gt; (int) 4, (int) 4 =&gt; (int) 5, (int) 5 =&gt; (int) 6, (int) 6 =&gt; (int) 7, (int) 7 =&gt; (int) 8, (int) 8 =&gt; (int) 9, (int) 9 =&gt; (int) 10, (int) 10 =&gt; (int) 11, (int) 11 =&gt; (int) 12, (int) 12 =&gt; (int) 13, (int) 13 =&gt; (int) 14 ] </pre> </div>{ "res": [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 ] }"

我们可以看到 body 中包含了数组,但是由于 body 中包含的 html,它会显示错误。 我的问题是如何仅在响应正文中获取数组并删除所有无用的 html。

【问题讨论】:

    标签: php jquery ajax cakephp controller


    【解决方案1】:

    您是否尝试过使用布局 false 和 autoRender false ?试试下面的代码,它可能会解决你的问题。

    public function test(){
        $this->layout = false;
        $this->autoRender = false;
    }
    

    【讨论】:

    • 响应正文没有任何变化,Html 仍然存在。
    • 有什么错误,你在 HTML 标签的 firebug 中看到了吗?
    【解决方案2】:

    如果您想让它自动解析,我建议您按照本书在以下链接中的建议进行操作:Routing File ExtensionRequest Handler

    所以,只需像这样在应用控制器的初始化中初始化请求处理程序:

    public function initialize()
    {
        parent::initialize();
        $this->loadComponent('RequestHandler');
    } 
    

    然后,告诉你的 routes.php 像这样解析扩展:

    Router::extensions(['json', 'html', 'xml']); // whatever extensions you want
    

    在这一切之后,您将希望将您的 ajax 调用 url 设置为:feuilles/test.json 以获取 json 中的结果。

    我还建议像这样在控制器中序列化结果:

    $this->set('res', $res);
    $this->set('_serialize', ['res']);
    

    【讨论】:

      【解决方案3】:

      @Theasc721,@Alimon Karim 的意思是你必须这样做

      public function test(){
          $this->layout = false;
          $this->autoRender = false;
          $x = $this->request->data['x'];
          $y = $this->request->data['y'];
          $actualFeuille = $this->request->session()->read('actualFeuille');
          $res = $actualFeuille->getListOfPossibilies(0,2);
          //please make sure you are providing an array for this.
          echo json_encode($res);  
      }
      

      我希望这对你有帮助。

      【讨论】:

      • 我已经应用了它,在测试功能中还有另一个内容,这是它的内容:public function test(){ $this-&gt;layout = false; $this-&gt;autoRender = false; $array = array("Name" =&gt; "John"); echo json_encode($array); exit; }
      • 这是我在console.log(result)时在ajax请求的错误函数中看到的返回响应:Object {readyState: 4, responseText: "&lt;pre class="cake-error"&gt;&lt;a href="javascript:void(0…dex.php, line 37&lt;/pre&gt;&lt;/div&gt;&lt;/pre&gt;{"Name":"John"}", status: 200, statusText: "OK"}abort: (a)always: ()complete: ()done: ()error: ()fail: ()getAllResponseHeaders: ()getResponseHeader: (a)overrideMimeType: (a)pipe: ()progress: .....
      • 你能给出你的 javascript 和 cakephp 控制器代码,看看有什么问题吗?
      • 我想我已经弄清楚问题出在哪里,问题实际上来自实体Feuille的方法getListOfPossibilies()返回的值,当我删除调用的行时这种方法效果很好。有谁知道如何解决这个问题?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-11
      • 1970-01-01
      • 2020-07-15
      相关资源
      最近更新 更多