doushupu2521 2015-11-24 23:00
浏览 29

CakePhp 3:Ajax - 使用html块返回数组

This ajax request send two parameters x and y to a php controller :

           $.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"
               }
           });

here's is the php controller :

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 get an array here's its content :

[
(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
]

When I get back the response it shows that there's an error, the fact is that the body recieved in the response html content just like the following example :

"<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 ] }"

We can see that the body got the array in it but because of the html contained in the body it displays an error. My question is what to do to only get the array in the body of the response and remove all the useless html.

  • 写回答

3条回答 默认 最新

  • duanchi5078 2015-11-25 01:35
    关注

    Did you try this by using layout false and autoRender false ? Try this below code, it may solve your problem.

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

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行