最も簡単な方法は、コンテキストスイッチを使用することです。お使いのコントローラでは、セットアップの「JSON」コンテキスト
class EntryController extends Zend_Controller_Action
{
public function init()
{
$this->_helper->ajaxContext->addActionContext('get-member-course', 'json')
->initContext();
}
public function getMemberCourseAction()
{
$id = $this->_getParam('id');
$this->view->test = array('test' => 'bleh');
}
}
呼び出し元のスクリプトのためのビューを使用して、アクションのためのAjaxContextヘルパーはJSONのURLへの参照を含める必要があります。たとえば、デフォルトではこの
$('#get-json').click(function() {
var url = this.href;
$.getJSON(url, {
"format": "json" // this is required to trigger the JSON context
}, function(data, textStatus, jqXHR) {
// handle response here
});
});
、JSONコンテキストのようなものを持っているでしょう。この
<a id="get-json" href="<?php echo $this->url(array(
'action' => 'get-member-course',
'controller' => 'entry',
'id' => $someId
), null, true) ?>">Click me for JSON goodness</a>
あなたのクライアント側のコードのようなリンクを作成し、あなたのJSONコードは、リンクをクリックすることで解雇されたと言います任意のビュープロパティがJSONとしてシリアル化され、レスポンスで返されます。あなたのビューのプロパティを簡単に変換できない場合は、自動JSONのシリアル化を無効にする必要があります...
$this->_helper->ajaxContext->addActionContext('my-action', 'json')
->setAutoJsonSerialization(false)
->initContext();
とJSONビュースクリプトを
// controllers/my/my-action.json.phtml
$simplifiedArray = array(
'prop' => $this->someViewProperty->getSomeValue()
);
echo Zend_Json::encode($simplifiedArray);
を提供し、私の更新の答えを参照してください。また、クライアント側のスクリプト – Phil