Sendig ajax request via zend


#1

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


#2

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 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 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 for the route configuration.

Let me know if you need more information.


#3

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.


#4

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:

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.


#5

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.


#6

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;

#7

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.


#8

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


#9

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.