# How to work with AJAX and SLIM?

**URL:** https://discourse.slimframework.com/t/how-to-work-with-ajax-and-slim/821
**Category:** Questions
**Created:** [October 20, 2016, 1:32pm UTC](https://discourse.slimframework.com/t/how-to-work-with-ajax-and-slim/821 "2016-10-20T13:32:54Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![haizone](https://avatars.discourse-cdn.com/v4/letter/h/ad7895/32.png) [@haizone](https://discourse.slimframework.com/u/haizone)
#### Post date: [October 20, 2016, 1:32pm UTC](https://discourse.slimframework.com/t/how-to-work-with-ajax-and-slim/821/1 "2016-10-20T13:32:54Z")

</div>

plz  
i need example how to work with ajax and slim  
with rout and the classes

tahnks:)

---

<div class="post-metadata">

### Author: ![TheLobos](https://avatars.discourse-cdn.com/v4/letter/t/e9bcb4/32.png) [@TheLobos](https://discourse.slimframework.com/u/TheLobos)
#### Post date: [October 20, 2016, 10:25pm UTC](https://discourse.slimframework.com/t/how-to-work-with-ajax-and-slim/821/2 "2016-10-20T22:25:22Z")

</div>

Route can be any route you want, although it makes sense to group Ajax functions into an AjaxController and give all Ajax entry points a path like

```
/ajax/yourFunc

```

The important thing when dealing with Ajax is to return a JSON response like this:

```
return $response->withJson (['error'=>1, 'errorCode'=>'InvalidID'], 200);

```

All in all, it’s quite easy.

---

<div class="post-metadata">

### Author: ![haizone](https://avatars.discourse-cdn.com/v4/letter/h/ad7895/32.png) [@haizone](https://discourse.slimframework.com/u/haizone)
#### Post date: [October 20, 2016, 10:48pm UTC](https://discourse.slimframework.com/t/how-to-work-with-ajax-and-slim/821/3 "2016-10-20T22:48:01Z")

</div>

im sorry i not understand  
plz u can do exmple with a parametrs? (in the class and in the AJAX and where i put that ajax)

tahks alot

---

<div class="post-metadata">

### Author: ![TheLobos](https://avatars.discourse-cdn.com/v4/letter/t/e9bcb4/32.png) [@TheLobos](https://discourse.slimframework.com/u/TheLobos)
#### Post date: [October 20, 2016, 11:35pm UTC](https://discourse.slimframework.com/t/how-to-work-with-ajax-and-slim/821/4 "2016-10-20T23:35:03Z")

</div>

This is not easy, because I use my own framework which has a lot of stuff built-in and it won’t make sense if I just copy/paste it. But here’s what an ajax controller function looks like:

```
public function setAdminMenu (Request $request, Response $response, $args)
{
    $id = Input::fromPost ('id'); 
    if (!$id) {
        return $response->withJson (['error'=>1, 'errorCode'=>'InvalidID'], 200);
    }

    // do something here ...

    $rc = ['success'=>1];
    return $response->withJson ($rc, 200);
}

```

You can map an Ajax request to a controller function like any other route. Hopefully this helps.
