Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
CakePHP Jquery.ajax()_Php_Jquery_Ajax_Cakephp - Fatal编程技术网

CakePHP Jquery.ajax()

CakePHP Jquery.ajax(),php,jquery,ajax,cakephp,Php,Jquery,Ajax,Cakephp,嗯,我是CakePHP新手。因此,调试这一点是痛苦的一天。这是我的代码: 模板\u controller.php function reajax($id = NULL) { $this->layout = false; $this->Template->id = $id; $template = $this->Template->read(); $this->set('res

嗯,我是CakePHP新手。因此,调试这一点是痛苦的一天。这是我的代码:

模板\u controller.php

    function reajax($id = NULL) {       
        $this->layout = false;
        $this->Template->id = $id;
        $template = $this->Template->read();
        $this->set('result', $template['Template']['content']);
    }
echo $result;
reajax.ctp

    function reajax($id = NULL) {       
        $this->layout = false;
        $this->Template->id = $id;
        $template = $this->Template->read();
        $this->set('result', $template['Template']['content']);
    }
echo $result;
js文件

$(document).ready(function() {
       $(".abcd").click(function(event){
           event.preventDefault();
           var id = this.id;
           $.ajax({
               type:"GET",
               url:"/templates/reajax/" + id,
               success : function(data) {
                   alert('success');
                   $("textarea").text(data);
               },
               error : function() {
                   alert(id);
               },
           })
       });
})
    <ul class="content-box-tabs">
      <?php echo $html->link($html->image('thumbnails/'.$template['Template']['thumbnail'], array('alt' => 'test', 'height' => '120', 'width' => '110')), array('controller' => 'templates', 'action' => 'reajax'), array('class' => 'abcd', 'id' => $template['Template']['id'], 'escape' => false))?> 
    </ul>
单击文件

$(document).ready(function() {
       $(".abcd").click(function(event){
           event.preventDefault();
           var id = this.id;
           $.ajax({
               type:"GET",
               url:"/templates/reajax/" + id,
               success : function(data) {
                   alert('success');
                   $("textarea").text(data);
               },
               error : function() {
                   alert(id);
               },
           })
       });
})
    <ul class="content-box-tabs">
      <?php echo $html->link($html->image('thumbnails/'.$template['Template']['thumbnail'], array('alt' => 'test', 'height' => '120', 'width' => '110')), array('controller' => 'templates', 'action' => 'reajax'), array('class' => 'abcd', 'id' => $template['Template']['id'], 'escape' => false))?> 
    </ul>

我不确定这个字符串

$this->layout = false;
像这样创建新的空布局ajax.ctp

<?=$content_for_layout?>
function reajax($id = NULL) {       
        $this->autoRender= false;
        $result = "Some value";
        echo $result;`enter code here`
    }
还有。。。。对于ajax请求,您可以尝试使用这种方式

$.get('/controller/action/'+Math.random(),{},function(data){
  $('#result').html(data);
});

出现错误的原因总是因为您从未从操作返回响应,您必须执行json的回显。例如,您只需设置数据即可


您还应该在控制器方法中进行某种验证,如果提供ID的模板不存在,会发生什么情况?您将得到一个错误,并且不会处理它。

首先,您应该回显您的结果,然后退出函数或使其autoRender=false;
对于调试,您应该使用开发者工具。

1。
$this->autoRender=false
$this->viewBuilder->layout('ajax')(对于Cakephp 3.0,在布局文件夹中创建一个ajax.ctp)
ajax.ctp
应该是

<?php
 echo $this->fetch('content') ;
 ?>
  • 您应该将echo$result放入reajax函数中
  • 现在,您的代码应该如下所示

    <?=$content_for_layout?>
    
    function reajax($id = NULL) {       
            $this->autoRender= false;
            $result = "Some value";
            echo $result;`enter code here`
        }
    

    这将对您有所帮助。

    我添加了一些关于ajax查询的示例,这是我通常的做法,顺便问一下,您是否使用了一些调试器?对不起,我只是使用了Firebug。事实上,我认为ajax url中存在一些错误。我甚至无法在PHP文件中获取参数“id”。最后我成功了,但我不知道为什么。检查编辑的帖子。还是谢谢你!您是否已使用Firebug检查错误结果?ajax响应的内容使它获得了成功。但是js文件中有一个有趣的代码。检查编辑的帖子。非常感谢。您可能应该对结果进行json_编码。谢谢您的建议。我将在控制器和jaso数据类型返回中生成一个强代码。干杯