# ZF1 Webservice responding with a JSON to an AJAX or API call

**URL:** <https://discourse.laminas.dev/t/zf1-webservice-responding-with-a-json-to-an-ajax-or-api-call/533>\
**Category:** Components & MVC\
**Created:** [March 16, 2018, 10:42pm UTC](https://discourse.laminas.dev/t/zf1-webservice-responding-with-a-json-to-an-ajax-or-api-call/533 "2018-03-16T22:42:35Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![David\_Ballerini](https://avatars.discourse-cdn.com/v4/letter/d/c5a1d2/32.png) [@David\_Ballerini](https://discourse.laminas.dev/u/David_Ballerini)\
**Post date:** [March 16, 2018, 10:42pm UTC](https://discourse.laminas.dev/t/zf1-webservice-responding-with-a-json-to-an-ajax-or-api-call/533/1 "2018-03-16T22:42:35Z")

</div>

Hi all,

I need to create some webservices that will be called by either ajax calls from within our site or API calls coming from another server, and will reply with a JSON.

Is there an established best practice?

Digging in the code i found two different ways to do that:

if( $this-\>\_getParam(‘sendNow’, true) )  
{  
return $this-\>\_helper-\>json($data);  
}  
else  
{  
$this-\>\_helper-\>viewRenderer-\>setNoRender(true);  
$data = Zend\_Json::encode($data);  
$this-\>getResponse()-\>setBody($data);  
}

I assume the difference must be in how the dispatchment loop will proceed after the generation of the JSON but I’m not sure - do you know?

---

<div class="post-metadata">

**Author:** ![ocramius](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/ocramius/32/23_2.png) [@ocramius](https://discourse.laminas.dev/u/ocramius)\
**Post date:** [March 17, 2018, 9:53am UTC](https://discourse.laminas.dev/t/zf1-webservice-responding-with-a-json-to-an-ajax-or-api-call/533/2 "2018-03-17T09:53:15Z")

</div>

Please don’t use ZF1 for new development work - it is unmaintained and abandoned since 2 years. [https://framework.zend.com/blog/2016-06-28-zf1-eol.html](https://framework.zend.com/blog/2016-06-28-zf1-eol.html)

---

<div class="post-metadata">

**Author:** ![David\_Ballerini](https://avatars.discourse-cdn.com/v4/letter/d/c5a1d2/32.png) [@David\_Ballerini](https://discourse.laminas.dev/u/David_Ballerini)\
**Post date:** [March 19, 2018, 2:30pm UTC](https://discourse.laminas.dev/t/zf1-webservice-responding-with-a-json-to-an-ajax-or-api-call/533/3 "2018-03-19T14:30:45Z")

</div>

We are aware of that - but Social Engine is a quite robust and independently maintained platform. Any hint regarding the specific topic?

---

<div class="post-metadata">

**Author:** ![froschdesign](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/froschdesign/32/38_2.png) [@froschdesign](https://discourse.laminas.dev/u/froschdesign)\
**Post date:** [March 19, 2018, 7:24pm UTC](https://discourse.laminas.dev/t/zf1-webservice-responding-with-a-json-to-an-ajax-or-api-call/533/4 "2018-03-19T19:24:33Z")

</div>

> [@David\_Ballerini](#):
>
> Any hint regarding the specific topic?

Your current usage is too complicated. You can simplify the code:

```auto
$sendNow = $this->_getParam('sendNow', true);

return $this->_helper->json($data, $sendNow);

```

And the rest is done by the helper.

All parameters are documented here: [Action Helpers - JSON](https://framework.zend.com/manual/1.12/en/zend.controller.actionhelpers.html#zend.controller.actionhelpers.json)  
Look also in the [code of the helper](https://github.com/zendframework/zf1/blob/master/library/Zend/Controller/Action/Helper/Json.php#L126).

* * *

> Social Engine is a quite robust and independently maintained platform

That does not change the fact that the underlying framework is abandoned and unmaintained.

---

<div class="post-metadata">

**Author:** ![David\_Ballerini](https://avatars.discourse-cdn.com/v4/letter/d/c5a1d2/32.png) [@David\_Ballerini](https://discourse.laminas.dev/u/David_Ballerini)\
**Post date:** [March 19, 2018, 8:31pm UTC](https://discourse.laminas.dev/t/zf1-webservice-responding-with-a-json-to-an-ajax-or-api-call/533/5 "2018-03-19T20:31:51Z")

</div>

That’s what makes me even more grateful for the help I can get - as I said, I don’t have a say in the basic decision.

---

<div class="post-metadata">

**Author:** ![froschdesign](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/froschdesign/32/38_2.png) [@froschdesign](https://discourse.laminas.dev/u/froschdesign)\
**Post date:** [March 20, 2018, 7:03am UTC](https://discourse.laminas.dev/t/zf1-webservice-responding-with-a-json-to-an-ajax-or-api-call/533/6 "2018-03-20T07:03:59Z")

</div>

Version 2 of ZF was released on 6 September 2012. With this date, the version 1 should be supported for a maximum of 24 months. But at the end it was 48 months. The end of life was on 28 September 2016.  
So please keep in mind, that it has been a long time since most of us have actively worked with the version 1. And we must not forget the current and new versions, which also need to get support.

The result of this: The support is very limited or not available at this point.
