I'm trying to learn how to use oop in php. I'm also fairly new to jquery. Is it possible to make an Ajax request to a php class method? I've only ever sent Ajax requests to a file specifically for that purpose and that returns the data I need.
相关问题
- Views base64 encoded blob in HTML with PHP
- how to define constructor for Python's new Nam
- Laravel Option Select - Default Issue
- Carriage Return (ASCII chr 13) is missing from tex
- PHP Recursively File Folder Scan Sorted by Modific
Although I would recommend at least understanding how to achieve this manually, you can always use Xajax which allows you to create a php class that is indirectly accessible from your page's javascript.
Basic answer no, but it can be done easily.
An ajax call itself cannot call class methods. It has not way of initiating the class and then calling the method. It can only cause the PHP file to run on the server via a POST/GET call on X url.
What you can do is use another file to act as a go-between from the ajax to the method. In other words, instead of calling the php file (update.php for example) directly as simple examples show, you can call a go-between file (call it whatever you like, ajax server, ajax router, etc) that uses GET/POST parameters you send to figuere out what method you want to use and what values to pass to it. You could then run the method from that file (initiating the class and calling the method with parameters), and return the results of that method from that file to the ajax calling script.