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
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.
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:
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:
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.
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:
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.