Zend:ContextSwitch Json回来了

时间:2010-05-19 08:53:58

标签: json zend-framework

在我的初学者中我有:

   $contextSwitch   = $this->_helper->getHelper('ForceContext');
   $contextSwitch->addActionContext('favourite-listing', 'json')->initContext();

在我的控制器方法中,我有:

    public function favouriteListingAction() {

    $newValues = array();

    if (isset($_POST['Playlist']) && is_array($_POST['Playlist'])) {
        foreach ($_POST['Playlist'] as $key => $value) {
            $response = $this->checkAction('Playlist', $value, 0, 1, 'favourite');
            $newValues['Playlist'][$value] = $response;
        }
    }

    if (isset($_POST['Clip']) && is_array($_POST['Clip'])) {
        foreach ($_POST['Clip'] as $key => $value) {
            $response = $this->checkAction('Clip', $value, 0, 1, 'favourite');
            $newValues['Clip'][$value] = $response;
        }
    }
    $this->view->favourites = $newValues;
}

然而,调用它,返回:

    {"loggedIn":true,"request":{},"response":{"headersSentThrowsException":true},"translate":{},"favourites":{"Clip":{"726":"<img src=\"\/design\/images\/icon\/subscribe.png\" \/> Add Clip To Favourites","727":
"<img src=\"\/design\/images\/icon\/subscribe.png\" \/> Add Clip To Favourites","728":"<img src=\"\/design\/images\/icon\/subscribe.png\" \/> Add Clip To Favourites","729":
"<img src=\"\/design\/images\/icon\/subscribe.png\" \/> Add Clip To Favourites","730":
"<img src=\"\/design\/images\/icon\/subscribe.png\" \/> Add Clip To Favourites","731":"<img src=\"\/design\/images\/icon\/subscribe.png\" \/> Add Clip To Favourites","732":
"<img src=\"\/design\/images\/icon\/subscribe.png\" \/> Add Clip To Favourites"}}}

正如您所看到的,它添加了斜杠,这会导致问题。有没有阻止这样做?

3 个答案:

答案 0 :(得分:2)

服务器上配置了可能的魔术引号?

Magic Quotes

答案 1 :(得分:1)

在我看来,这种行为是正常的,你的JSON解析器应该删除这些斜杠。我个人使用JQuery $ .ajax()函数用于我的Ajax项目,你必须将dataType设置为JSon(我的同事之一在尝试将结果作为文本并以其他方式解析时遇到问题)。

$.ajax( ... ,dataType : "json", ...);

答案 2 :(得分:0)

如果你没有发现问题,你可以随时在数据上运行函数stripslashes http://php.net/manual/fr/function.stripslashes.php来剥去它的额外斜杠。