Does anyone know how to do this?
I want to point $.getJson() to a controller and have it return json through ajax based on request arguments. Unfortunately it appears Zend handles the get parameters different than jQuery encodes them.
How can I do this with Zend and jQuery? I saw something on stackoverflow about Post arguments but I am lost when it comes to GET.
When using jQuery I get a 404 error using this code:
Client side:
$.getJSON("/entry/get-member-course",
{
"id": 1,
"format": "json"
},
function(json) {
alert("WIN");
});
Server side:
public function init() {
$this->_helper->ajaxContext->addActionContext('get-member-course', 'json')->initContext();
}
public function getMemberCourseAction() {
$this->view->test = Array("test"=>"bleh");
}
Easiest way is to use context switching. In your controller, setup the AjaxContext helper for your action with a “json” context
The view for the calling script should contain a reference to the JSON URL. For example, say your JSON code is fired by clicking a link, create the link like this
Your client-side code would have something like this
By default, when the JSON context is triggered, any view property is serialized as JSON and returned in the response. If your view properties cannot be simply converted, you need to disable automatic JSON serialization…
and provide a JSON view script