Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/286.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(),数据类型:json_Php_Jquery_Ajax_Json_Cakephp - Fatal编程技术网

cakephp,jquery,.ajax(),数据类型:json

cakephp,jquery,.ajax(),数据类型:json,php,jquery,ajax,json,cakephp,Php,Jquery,Ajax,Json,Cakephp,是否可以不使用cakephp控制器函数的视图?我试图让我的服务器返回一个不是字符串而是数组的数据类型 我的控制器功能: function test() { $this->layout = 'plain'; $task['Numbers']['uno'] = 'mooo'; $task['Numbers']['dos'] = 'says the cow'; $result = json_encode($task); $this

是否可以不使用cakephp控制器函数的视图?我试图让我的服务器返回一个不是字符串而是数组的数据类型

我的控制器功能:


    function test() {
      $this->layout = 'plain';
      $task['Numbers']['uno'] = 'mooo';
      $task['Numbers']['dos'] = 'says the cow';
      $result = json_encode($task);
      $this->set('result', $result);
    }


    $('#test').live('click', test);

    function test() {

        var data = $('#form').serialize();

        $.ajax({
        type: "post",
        url: "/controller/test",
        data: data,
        dataType: 'json',
        success: function(response){            
        alert(response.Numbers.uno);
        }
        });

    }
我的视图文件test.ctp



    echo $result;



    $('#test').live('click', test);

    function test() {

        var data = $('#form').serialize();

        $.ajax({
        type: "post",
        url: "/controller/test",
        data: data,
        dataType: 'json',
        success: function(response){            
        alert(response.Numbers.uno);
        }
        });

    }
我的jquery:


    function test() {
      $this->layout = 'plain';
      $task['Numbers']['uno'] = 'mooo';
      $task['Numbers']['dos'] = 'says the cow';
      $result = json_encode($task);
      $this->set('result', $result);
    }


    $('#test').live('click', test);

    function test() {

        var data = $('#form').serialize();

        $.ajax({
        type: "post",
        url: "/controller/test",
        data: data,
        dataType: 'json',
        success: function(response){            
        alert(response.Numbers.uno);
        }
        });

    }

和更改
)
alert(response.Numbers.uno);

有人知道会发生什么吗?

查看cakePHP关于请求处理的书籍:


CakePHP有一些内置功能来处理返回json的控制器方法。

首先,回答您的问题,是的,您可以不使用视图(这是一个很好的例子)。在控制器操作中(
test
,在本例中),只需设置
$this->autoRender=false。这就是我通常做的,只是在控制器本身中回显编码的JSON字符串。这样会减少混乱

接下来我要看的是
config.php
中的调试设置。如果未设置为0,则很有可能这是您的问题。echo语句可能会回显JSON格式的字符串,但会将调试输出附加到该字符串。在方法中,只需包含
Configure::write('debug',0)
。这将仅对当前请求禁用调试。我通常在AppController中执行此操作,实际上:

if ( $this->RequestHandler->isAjax() ) {
   Configure::write ( 'debug', 0 );
}

我会使用像Firebug这样的工具,看看您的ajax请求发生了什么变化。它将监视这些请求,并提供请求和响应头供您检查。这可能会有帮助。如果是后者的问题,那么Firebug会在响应标题中向您显示这一点。

我同意Rob的观点,除了在应用程序控制器中设置调试。这让我有点恼火,因为每次你想调试某个特定函数时,都必须在app controller中设置调试级别。我在我工作的控制器中这样写我的处理程序

Configure::write('debug', 0);
$this->autoRender = false;

我也同意使用firebug。使用firebug,您只需打开firebug,单击控制台,然后运行测试。这将告诉您正在使用jquery做什么,并给出结果。

您必须在使用数据之前转换数据。
看看这个答案:

你必须使用

var json = $.parseJSON(response);
alert(json.Numbers.uno);
我只是用

die(json_encode(array('success' => true, 'result' => $result)));

在被调用的控制器方法的末尾,它对我有效。

我正在使用$.post,并将“json”保留在末尾,如下所示-
$.post(url,函数(数据){if(数据){alert(“yes”+data.success);alert(数据);},“json”)我在第一个警报(alerting data.success)中未定义,并在第二个警报(alerting data)中获得包含所有回音的字符串输出。我有一个类似的问题,如OP,我使用$.ajax,如我在对OP问题的评论中所述。如果我只添加这个条件
If($this->RequestHandler->isAjax()){Configure::write('debug',0);}
,我的控制器逻辑中就不会再显示任何内容了。因此,在if条件中放置回显也不会回显。还有什么办法解决我的问题吗?与OP相同-无法使用alert(data.success)读取$.post成功处理程序中的json数据,但能够使用alert(data)读取所有回音