开发者

Zend Json_Encode and .getJson success not being called?

开发者 https://www.devze.com 2023-04-01 21:32 出处:网络
I have a script that I am using to return some JSON from PHP using the Zend Framework. This is what I get back: []{\"Test\":\"Tester\"} but the success function in jQuery isn\'t being called. Shouldn

I have a script that I am using to return some JSON from PHP using the Zend Framework.

This is what I get back: []{"Test":"Tester"} but the success function in jQuery isn't being called. Shouldn't the json be [{"Test":"Te开发者_高级运维ster"}]?

In my controller I did this:

echo Zend_Json::encode(array("Test" => "Tester"));

Client Js:

  $.getJSON("/entry/get-projects",
    {
        "id": 1,
        "format": "json"
    },
    function(data, textStatus, jqXHR) {

        alert("win");
    }
    );


In your action that is handling the json call

$callback = $this->getRequest()->getParam('callback');
echo $callback . '(' . Zend_Json::encode(array('key' => 'value')) . ')';

In your JS

var map = {
    'key1'  : val1,
    'key2'  : val2
};

$.post('/some/ajax-action/', map, some_callback_function, 'json');

Failing that, you could always try:

http://php.net/manual/en/function.json-encode.php
echo json_encode(array('key' => 'value'));


I got it to work using $this->_helper->json(); as mentioned here: http://www.stoimen.com/blog/2010/08/13/returning-json-in-a-zend-controllers-action/

Not sure if there is a better way. For example: Propel has a nifty toJSON method that kicks back json code from your query result. It would suck to convert it to an array using propel and then have to convert it to JSON with Zend.

0

精彩评论

暂无评论...
验证码 换一张
取 消