# Ajax Response not returning

**URL:** <https://discourse.cakephp.org/t/ajax-response-not-returning/6410>\
**Category:** Need Help\
**Created:** [July 25, 2019, 7:28am UTC](https://discourse.cakephp.org/t/ajax-response-not-returning/6410 "2019-07-25T07:28:07Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bird87](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/bird87/32/405_2.png) [@Bird87](https://discourse.cakephp.org/u/Bird87)\
**Post date:** [July 25, 2019, 7:28am UTC](https://discourse.cakephp.org/t/ajax-response-not-returning/6410/1 "2019-07-25T07:28:07Z")

</div>

I have the following ‘handler’ for ajaxRequests in my AppController:

```
/**
 * @param array $messages
 * @return Response
 */
public function ajaxSuccessResponse(array $messages = []): Response
{
    return $this->ajaxResponse([
        'status' => 'success',
        'messages' => $messages
    ]);
}

/**
 * @param array $messages
 * @return Response
 */
public function ajaxErrorResponse(array $messages = []): Response
{
    return $this->ajaxResponse([
        'status' => 'error',
        'messages' => $messages
    ]);
}

/**
 * @param array $response
 * @return Response
 */
private function ajaxResponse(array $response): Response
{
    $this->viewBuilder()->setTemplate('ajax');
    $this->response->withType('json');
    $this->response->withStringBody(json_encode($response));
    return $this->response;
}

```

When I call a method that needs to handle ajax requests, I call it like so:

```
return $this->ajaxErrorResponse(['Some error message.']);

```

When I use the below javascript, my response is empty (when I check it on Google Chrome’s network tab)

```
$.post({
    url: 'my-url',
    headers: {
        'X-CSRF-Token': csrfToken
    },
    data: {
        username: $('input[name="username"]').val(),
        password: $('input[name="password"]').val(),
        url: $('input[name="url"]').val()
    },
    dataType: "json"
});

```

What am I doing wrong? If I echo the response, I get an exception that says result should be either Response or null.

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [July 25, 2019, 5:49pm UTC](https://discourse.cakephp.org/t/ajax-response-not-returning/6410/2 "2019-07-25T17:49:16Z")

</div>

The response object is [immutable](https://book.cakephp.org/3.0/en/controllers/request-response.html#common-mistakes-with-immutable-responses). Try this:

```auto
private function ajaxResponse(array $response): Response
{
    $this->viewBuilder()->setTemplate('ajax');
    return $this->response
        ->withType('json')
        ->withStringBody(json_encode($response));
}
```

---

<div class="post-metadata">

**Author:** ![Bird87](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/bird87/32/405_2.png) [@Bird87](https://discourse.cakephp.org/u/Bird87)\
**Post date:** [August 13, 2019, 1:08pm UTC](https://discourse.cakephp.org/t/ajax-response-not-returning/6410/3 "2019-08-13T13:08:30Z")

</div>

Thank you, this worked.
