如何从jQuery ajax调用PHP方法/函数?

如何从jQuery ajax调用PHP方法/函数?这有可能实现吗? 如果在jquery中是不可能的,那么在纯JavaScript中是可能的吗? 我已在网上搜索了很多资料,试图找到答案,但找不到任何有帮助的内容。

我是否需要向另一个php文件发出ajax请求,为此设置一个专用的会话变量,然后发出另一个ajax请求,并在所请求的php文件中使用会话数据来执行我的操作? 感谢您提供的任何帮助!

An AJAX call is the same as loading the URL in your browser. So, you need to make a way to have your PHP script call the right function based on the URL, such as $_GET variables.

For example:

<?php
    if(isset($_GET['func'])){
       $func = $_GET['func'];
       if($func === 'a') a();
       if($func === 'b') b();
    }

Then make an AJAX call to: http://example.com/script.php?func=a.

$.ajax({
    url: 'http://example.com/script.php',
    type: 'GET',
    data: {func: 'a'},
    success: function(data){
        console.log(data);
    }
});

very simply, jquery:

$.get("code.php");

PHP:

<?php func(); ?>

for advanced: use this jquery to run php:

$.get("code.php?action=func1");

and write this code in php:

<?php
if( $_REQUEST['action'] == 'func1' ){
   func1();
   die();
}
?>