Skip to main content

The proper way to send JSON data from the controller action is to do as such:

public function indexAction()
{
        $this->getResponse()->clearHeaders()->setHeader('Content-type','application/json',true);
        $this->getResponse()->setBody(json_encode($response));
}

...where $response is an object or an array.

However, the proper way to "consume" it or to use it depends on your application. The JSON 'decoding' is handled by your ajax method in the javascript. Here are a couple examples:

jQuery

$.ajax({
    dataType: 'json',
    data: /* your data serialized */
    url: '/route/controller/action'
}).done(function(response){
    console.log(response); // will return the json as an object
});

Prototype.js

new Ajax.Response({
    requestHeaders: {Accept: 'application/json'},
    onSuccess: function(response){
        var json = response.responseText.evalJSON(true);
        console.log(json);
    }
});

You can learn more about how to use Prototype's Ajax method in this screencast I produced: http://quick.as/7x6ska7 [end: shamelessplug]You can learn more about how to use Prototype's Ajax method in this screencast I produced: http://quick.as/7x6ska7 [end: shamelessplug]

The proper way to send JSON data from the controller action is to do as such:

public function indexAction()
{
        $this->getResponse()->clearHeaders()->setHeader('Content-type','application/json',true);
        $this->getResponse()->setBody(json_encode($response));
}

...where $response is an object or an array.

However, the proper way to "consume" it or to use it depends on your application. The JSON 'decoding' is handled by your ajax method in the javascript. Here are a couple examples:

jQuery

$.ajax({
    dataType: 'json',
    data: /* your data serialized */
    url: '/route/controller/action'
}).done(function(response){
    console.log(response); // will return the json as an object
});

Prototype.js

new Ajax.Response({
    requestHeaders: {Accept: 'application/json'},
    onSuccess: function(response){
        var json = response.responseText.evalJSON(true);
        console.log(json);
    }
});

You can learn more about how to use Prototype's Ajax method in this screencast I produced: http://quick.as/7x6ska7 [end: shamelessplug]

The proper way to send JSON data from the controller action is to do as such:

public function indexAction()
{
        $this->getResponse()->clearHeaders()->setHeader('Content-type','application/json',true);
        $this->getResponse()->setBody(json_encode($response));
}

...where $response is an object or an array.

However, the proper way to "consume" it or to use it depends on your application. The JSON 'decoding' is handled by your ajax method in the javascript. Here are a couple examples:

jQuery

$.ajax({
    dataType: 'json',
    data: /* your data serialized */
    url: '/route/controller/action'
}).done(function(response){
    console.log(response); // will return the json as an object
});

Prototype.js

new Ajax.Response({
    requestHeaders: {Accept: 'application/json'},
    onSuccess: function(response){
        var json = response.responseText.evalJSON(true);
        console.log(json);
    }
});

You can learn more about how to use Prototype's Ajax method in this screencast I produced: http://quick.as/7x6ska7 [end: shamelessplug]

added 8 characters in body
Source Link
philwinkle
  • 35.8k
  • 5
  • 91
  • 146

The proper way to send JSON data from the controller action is to do as such:

public function indexAction()
{
        $this->getResponse()->clearHeaders()->setHeader('Content-type','application/json',true);
        $this->getResponse()->setBody(json_encode($response));
}

...where $response is an object or an array.

However, the proper way to "consume" it or to use it depends on your application. The JSON 'decoding' is handled by your ajax method in the javascript. Here are a couple examples:

jQuery

$.ajax({
    dataType: 'json',
    data: /* your data serialized */
    url: '/route/controller/action'
}).done(function(response){
    console.log(response); // will return the json as an object
});

Prototype.js

new Ajax.Response({
    requestHeaders: {Accept: 'application/json'},
    onSuccess: function(response){
        var json = response.responseText.evalJSON(true);
        console.log(json);
    }
});

You can learn more about how to use Prototype's Ajax method in this screencast I produced: http://quick.as/7x6ska7 [end: shamelessplug]

The proper way to send JSON data from the controller action is to do as such:

public function indexAction()
{
        $this->getResponse()->clearHeaders()->setHeader('Content-type','application/json',true);
        $this->getResponse()->setBody(json_encode($response));
}

...where $response is an object or an array.

However, the proper way to "consume" it or to use it depends on your application. The JSON 'decoding' is handled by your ajax method in the javascript. Here are a couple examples:

jQuery

$.ajax({
    dataType: 'json',
    data: /* your data serialized */
    url: '/route/controller/action'
}).done(function(response){
    console.log(response); // will return the json as an object
});

Prototype.js

new Ajax.Response({
    requestHeaders: {Accept: 'application/json'},
    onSuccess: function(response){
        var json = response.responseText.evalJSON(true);
        console.log(json);
    }
});

You can learn more about how to use Prototype's Ajax method in this screencast I produced: http://quick.as/7x6ska7

The proper way to send JSON data from the controller action is to do as such:

public function indexAction()
{
        $this->getResponse()->clearHeaders()->setHeader('Content-type','application/json',true);
        $this->getResponse()->setBody(json_encode($response));
}

...where $response is an object or an array.

However, the proper way to "consume" it or to use it depends on your application. The JSON 'decoding' is handled by your ajax method in the javascript. Here are a couple examples:

jQuery

$.ajax({
    dataType: 'json',
    data: /* your data serialized */
    url: '/route/controller/action'
}).done(function(response){
    console.log(response); // will return the json as an object
});

Prototype.js

new Ajax.Response({
    requestHeaders: {Accept: 'application/json'},
    onSuccess: function(response){
        var json = response.responseText.evalJSON(true);
        console.log(json);
    }
});

You can learn more about how to use Prototype's Ajax method in this screencast I produced: http://quick.as/7x6ska7 [end: shamelessplug]

Source Link
philwinkle
  • 35.8k
  • 5
  • 91
  • 146

The proper way to send JSON data from the controller action is to do as such:

public function indexAction()
{
        $this->getResponse()->clearHeaders()->setHeader('Content-type','application/json',true);
        $this->getResponse()->setBody(json_encode($response));
}

...where $response is an object or an array.

However, the proper way to "consume" it or to use it depends on your application. The JSON 'decoding' is handled by your ajax method in the javascript. Here are a couple examples:

jQuery

$.ajax({
    dataType: 'json',
    data: /* your data serialized */
    url: '/route/controller/action'
}).done(function(response){
    console.log(response); // will return the json as an object
});

Prototype.js

new Ajax.Response({
    requestHeaders: {Accept: 'application/json'},
    onSuccess: function(response){
        var json = response.responseText.evalJSON(true);
        console.log(json);
    }
});

You can learn more about how to use Prototype's Ajax method in this screencast I produced: http://quick.as/7x6ska7