CakePHP的,jQuery的,阿贾克斯(),数据类型:JSON [英] cakephp, jquery, .ajax(), dataType: json

查看:110
本文介绍了CakePHP的,jQuery的,阿贾克斯(),数据类型:JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是有可能能够做到没有一个CakePHP的控制器功能的看法?我想有我的服务器返回一个数据类型,它不是一个字符串 - 但一个数组

is it possible to be able to do without a view for a cakephp controller function? i am trying to have my server return a datatype that is not a string - but an array

我的控制器功能:


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

我的视图文件test.ctp




    echo $result;


我jQuery的:




    $('#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);
        }
        });

    }

点击HTML元素标记试验并没有给我任何东西。但是如果我冒了出来

clicking on the html element marked test doesn't give me anything. however if i take out

dataType: 'json', 


和变更


and change

alert(response.Numbers.uno);


to

alert(response);


 在我的jQuery的 - 我得到一个警告:json的EN codeD数据,但作为一个字符串(


in my jquery - i get an alert: the json encoded data but as a string (

alert(typeof response);

没有任何人有任何想法可能发生的事?

does anyone have any idea what might be happening?

推荐答案

首先,要回答你的问题,是的,你可以没有一个视图(这是一个很好的情况下,这样做)。在你的控制器动作(测试,在这种情况下),只需设置 $这个 - > autoRender = FALSE; 。这就是我通常做,只是呼应了EN codeD JSON字符串在控制器中。更整洁的方式。

First, to answer your question, yes you can do without a view (and this is a good case for doing so). In your controller action (test, in this case), just set $this->autoRender = false;. That's what I usually do and just echo out the encoded JSON string in the controller itself. Less clutter that way.

接下来的事情,我想看看在的config.php 调试设置。如果没有设置为0,有一个非常好的机会,这是你的问题。您的回音声明可能呼应的JSON格式的字符串,但附加调试输出到它。在该方法中,只包括配置::写(调试,0)。这将禁用调试的只是当前请求的。我通常在我的AppController的实际:

The next thing that I'd look at are your debug settings in config.php. If not set to 0, there's a very good chance that this is your problem. Your echo statement may be echoing the JSON-formatted string, but appending debug output to it. In the method, just include Configure::write ( 'debug', 0 ). This will disable debug only for the current request. I usually do this in my AppController actually:

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

我搞类似Firebug的工具,看看发生了什么,以你的Ajax请求。它将监测这些要求,并提供了请求和响应头为你检查。这可能会有帮助。如果是后者的问题,那么萤火虫会显示您的响应头。

I'd engage a tool like Firebug and see what's happening to your ajax request. It will monitor those requests and provide the request and response headers for you to inspect. That may help. If the latter is the problem, then Firebug would have shown you that in the response headers.

这篇关于CakePHP的,jQuery的,阿贾克斯(),数据类型:JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆