# Sendig ajax request via zend

**URL:** <https://discourse.laminas.dev/t/sendig-ajax-request-via-zend/503>\
**Category:** Components & MVC\
**Created:** [March 1, 2018, 4:57pm UTC](https://discourse.laminas.dev/t/sendig-ajax-request-via-zend/503 "2018-03-01T16:57:48Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![erfan](https://avatars.discourse-cdn.com/v4/letter/e/c4cdca/32.png) [@erfan](https://discourse.laminas.dev/u/erfan)\
**Post date:** [March 1, 2018, 4:57pm UTC](https://discourse.laminas.dev/t/sendig-ajax-request-via-zend/503/1 "2018-03-01T16:57:48Z")

</div>

hey everybody!  
I want to send an ajax request to some file within Application\Model from my layout.phtml using code below:

```
$(document).ready(function(){
    $('a#items').click(function () {
var data={};
data.id =$(this).attr("data_id");
$.ajax({
    type : 'POST',
    url :'../../../module/Application/src/Model/somefile.php',
    data :data,
    success: function(data){
        alert(data)
    },
    error:function(){
        alert("fail );
    }
});
});
});

```

i assume that there is something wrong with the `url:'../../../module/Application/src/Model/somefile.php'` part how could it possible to achieve that!  
thank you

---

<div class="post-metadata">

**Author:** ![enrico](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/enrico/32/12_2.png) [@enrico](https://discourse.laminas.dev/u/enrico)\
**Post date:** [March 5, 2018, 3:54pm UTC](https://discourse.laminas.dev/t/sendig-ajax-request-via-zend/503/2 "2018-03-05T15:54:49Z")

</div>

The `url` used in the AJAX call with jQuery needs to be a valid URL. You need to use a valid URL for your application and not a relative path to `Model/somefile.php`.

I’m assuming you are building your application using the [ZendSkeletonApplication](https://github.com/zendframework/ZendSkeletonApplication) you can create a `Controller` that uses the `Model/somefile` that you wrote to return a 200 OK response, e.g. using a JSON output in the body if you need to send a message to the client.  
You can check the [IndexController](https://github.com/zendframework/ZendSkeletonApplication/blob/master/module/Application/src/Controller/IndexController.php) of the skeleton application to see how it works, in this case the URL will be `/` or `/application/index`, see the [config/module.config.php](https://github.com/zendframework/ZendSkeletonApplication/blob/master/module/Application/config/module.config.php#L15-L38) for the route configuration.

Let me know if you need more information.

---

<div class="post-metadata">

**Author:** ![erfan](https://avatars.discourse-cdn.com/v4/letter/e/c4cdca/32.png) [@erfan](https://discourse.laminas.dev/u/erfan)\
**Post date:** [March 6, 2018, 2:53pm UTC](https://discourse.laminas.dev/t/sendig-ajax-request-via-zend/503/3 "2018-03-06T14:53:35Z")

</div>

hi thanks for your reply, I gave up trying to pass anything to Model\somefile and wondering where I am wrong. here is my problem in more detail:  
I already had an separate controller for doing the task by the name of AjaxController.php residing in Application\Controller.  
To begin with, here is my visitCounter.js file appended to layout.phtml:

```
$(document).ready(function(){
        var data={};
        data.id = "bye";
        $.ajax({
            type : 'POST',
            url :'/Ajax/index',
            data :data
        });
});

```

and the AjaxController.php is as follows:

```
class AjaxController extends AbstractActionController
{
    private $menuId = 'hi';

    public function indexAction()
    {
        $tmp = $this->params()->fromPost();
        $this->menuId = $tmp;
        $view = new ViewModel([
           'menuId'=> $this->menuId,
        ]);
        $view->setTemplate('layout/layout');
        return $view;
    }
}

```

I just simply want to see if the visitCounter’s ajax works and get the `data` from it and output the value to the layout as a test like `print_r($this->menuId);`  
but nothing happened.  
for the record here is my Application’s `module.config.php` and the specific route:

```
    'ajax' => [
        'type' => Segment::class,
        'options' => [
            'route' => '/ajax[/:action]',
            'defaults' => [
                'controller' => Controller\AjaxController::class,
                'action' => 'index',
            ],
        ],
    ],

```

I really appreciate if you could help me.

---

<div class="post-metadata">

**Author:** ![enrico](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/enrico/32/12_2.png) [@enrico](https://discourse.laminas.dev/u/enrico)\
**Post date:** [March 6, 2018, 4:25pm UTC](https://discourse.laminas.dev/t/sendig-ajax-request-via-zend/503/4 "2018-03-06T16:25:56Z")

</div>

The Zend Framework code seems to be correct. Maybe, the error is coming from jquery code. Are you sure that the ajax call is running and passing the data? How are you running the PHP application?  
For testing purposes, you can just use the internal PHP webserver, run the following commands from the root of your ZF application:

```bash
php -S 0.0.0.0:8080 -t public public/index.php

```

Then you can open the application using the browser at [http://localhost:8080](http://localhost:8080).

---

<div class="post-metadata">

**Author:** ![davidmintz](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/davidmintz/32/213_2.png) [@davidmintz](https://discourse.laminas.dev/u/davidmintz)\
**Post date:** [March 8, 2018, 4:57pm UTC](https://discourse.laminas.dev/t/sendig-ajax-request-via-zend/503/5 "2018-03-08T16:57:54Z")

</div>

trying hitting the url directly with your browser and make sure the route is working. And, if you haven’t already, use the developer console in your browser so you can inspect xhr requests and responses.

Also, I notice that in your Javascript you’re spelling it /Ajax/index, i.e., with a capital A, but the route definition is lowercase A.

---

<div class="post-metadata">

**Author:** ![samsonasik](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/samsonasik/32/777_2.png) [@samsonasik](https://discourse.laminas.dev/u/samsonasik)\
**Post date:** [March 10, 2018, 11:52am UTC](https://discourse.laminas.dev/t/sendig-ajax-request-via-zend/503/6 "2018-03-10T11:52:15Z")

</div>

setTemplate(‘layout/layout’) will set the “content” as layout/layout, it seems wrong! You probably just need a json or a view content itself, if you want to return view content, you can call setTerminal from view:

```
$view->setTerminal(true);
return $view;
```

---

<div class="post-metadata">

**Author:** ![erfan](https://avatars.discourse-cdn.com/v4/letter/e/c4cdca/32.png) [@erfan](https://discourse.laminas.dev/u/erfan)\
**Post date:** [March 11, 2018, 2:18pm UTC](https://discourse.laminas.dev/t/sendig-ajax-request-via-zend/503/7 "2018-03-11T14:18:57Z")

</div>

thanks,everybody the problem solved and what is left to say is that I just needed to return `Json` using `JsonModel` to a page to diagnose the issue after that I found some minor problem in passing ajax data and retrieving in Controller as an Array. I really appreciate your help.

---

<div class="post-metadata">

**Author:** ![erfan](https://avatars.discourse-cdn.com/v4/letter/e/c4cdca/32.png) [@erfan](https://discourse.laminas.dev/u/erfan)\
**Post date:** [March 11, 2018, 2:20pm UTC](https://discourse.laminas.dev/t/sendig-ajax-request-via-zend/503/8 "2018-03-11T14:20:52Z")

</div>

> [@davidmintz](#):
>
> /Ajax/index

thanks, yes that is an important point to url and I corrected it.

---

<div class="post-metadata">

**Author:** ![erfan](https://avatars.discourse-cdn.com/v4/letter/e/c4cdca/32.png) [@erfan](https://discourse.laminas.dev/u/erfan)\
**Post date:** [March 11, 2018, 2:22pm UTC](https://discourse.laminas.dev/t/sendig-ajax-request-via-zend/503/9 "2018-03-11T14:22:26Z")

</div>

> [@samsonasik](#):
>
> setTemplate(‘layout/layout’) will set the “content” as layout/layout, it seems wrong! You probably just need a json or a view content itself, if you want to return view content, you can call setTerminal from view:

thank you, I didn’t want to pass any object back to the layout I just needed to send a parameter when click event on menu items occured.
