Attila Naghi
Attila Naghi

Reputation: 2686

Is there possible to print a message in a zend controller function, without using a view?

I don't know if this question is duplicated or not. I'm a totally newbie in zend. I want to use an ajax event and when i call the controllername/myfunctionfromthecontroller in url , i want to return a message . I tried this :

$.ajax({
    url: "/location/testare",
    type: "POST",
    data: post_array,
    success: function (data, textStatus, jqXHR) {
        //data - response from server
    },
    error: function (jqXHR, textStatus, errorThrown) {

    }
});

and in the controller :

public function testareAction()
{ 
    $this->view->String = 'hello world';
}

the firebu return me this : "500 Internal Server Error". Can someone help me with this ?

Upvotes: 1

Views: 211

Answers (3)

JocBro1
JocBro1

Reputation: 24

Use helpers!

Example:

If you want to return string using json:

$.ajax({
  ...
  dataType: 'json',
  ...
});

And in controller:

public function testareAction() 
{
    $this->_helper->json('hello world');
}

That's all!

More Zend Framework Helper Json

Upvotes: 0

MaiKaY
MaiKaY

Reputation: 4482

the best way is to use the jsonModel

it looks like

public function testareAction()
{
    return new JsonModel(array(
        'message' => 'hello world',
    ));
}

Upvotes: 0

Daniel W.
Daniel W.

Reputation: 32310

I use this in Zend Framework 1. MaiKaY's solution seems to be fore ZF2:

In the controller, before the output, set the header to expose json output:

    $this->getResponse()->setHeader('Content-Type', 'application/json');

Disable all the layout stuff:

    $this->_helper->layout->disableLayout();

And if you want to use echo \Zend_Json::encode($yourOutput); instead of using a view for the json, use this:

    $this->_helper->viewRenderer->setNoRender(true);

Upvotes: 4

Related Questions