jsonp和zend框架1不返回任何内容(我正在使用phonegap并使用Zend作为服务)

时间:2014-05-12 04:51:58

标签: zend-framework cordova jsonp

这是我的zend控制器 enter image description here

这就是我打电话给我的服务 enter image description here

请教我如何。这一点很重要 !非常感谢你

2 个答案:

答案 0 :(得分:0)

看起来像你在那里做了一些额外的工作。我在ZF1和ZF2中都是这样做的:

PHP

// notice 'searchteams' is lowercased. There's been problems in the past
// when camelcasing action names, by default I believe ZF is looking for
// a lowercase action name unless you've configured it otherwise
public function searchteamsAction()
{
    // make sure this is an ajax request (this is a method I usually write, if 
    // Zend has one you could use that here)
    if ($this->isXmlHttpRequest())
    {
        // instantiate model, get the results, good
        // disabling view and layout, good

        // headers, never really had an issue that needed the headers to be set here
        // would simply have..
        echo json_encode($res);
    }
}

JS:在AJAX调用的成功方法中,我这样做:

success : function (data){   
    // parse the json
    var parsed_data = $.parseJSON(data);
    console.log(parsed_data);

    // do stuff with parsed_data
}

希望有所帮助。

答案 1 :(得分:0)

  1. return var_dump($res);

  2. 之前使用$this->_response->setHeader(...检查您的数据
  3. 如果您有数据,请尝试添加$this->_response->setHeader('Content-Type', 'application/json', true);

  4. 如果不起作用,请尝试替换

    $this->_response->setHeader(...);
    $this->_response->setHeader(...);
    $this->_response->setHeader(...);
    $this->_response->setHeader(...);
    

    echo Zend_Json::encode($res);
    仅限

    return $this->_helper->json($res);
    
  5. 你也可以看到jQuery方面的错误:

    error: function(xhr, status, error) {
      alert("error status: " + status);
      var err = eval("(" + xhr.responseText + ")");
      alert(err.Message);
    }
    

    我希望它会对你有所帮助:)。