You can define a calling method on the page, as follows:
The code is as follows:
function getData(){ $.getJSON("http://123.123.123.123/?callback=?", { "m":"data",// 指定php的文件名字 "act":"getdata",// 指定php文件中的方法 "name":"问题儿童"// 传入的参数 }, function(data) { // 获得返回值 } }); }
The corresponding link (123.123.123.123) PHP files generally call the index.php file first by default. After processing through the methods in the index.php file, go to the corresponding PHP file, find the corresponding method, and execute it. The
index.php code is as follows:
The code is as follows:
<?php /** * 入口文件 */ $string = $_SERVER["REQUEST_URI"];// 获取访问的url $m = get_m($string); $file_path = "app/".$m.".php"; define('IS_INDEX',true);// 阻止直接访问app目录 require ($file_path); /** * * 获取访问php文件 * @param string $url */ function get_m($url){ $strings = explode('m=', $url); $res = explode("&", $strings[1]); return empty($res[0])?'index':$res[0]; } ?>
data.php code is as follows:
The code is as follows:
<?php /** * data文件 */ $act = !empty($_GET['act']) ? $_GET['act'] : ''; if ($act == 'getdata') { $name = "我的名字叫:".$_REQUEST['name']; echo $_REQUEST["callback"]."(".json_encode($name).")"; } ?>
After successful call, the screen can obtain the returned json data.
Related learning recommendations: php programming (video)
The above is the detailed content of Detailed explanation of Ajax cross-domain calling code using jQuery in PHP. For more information, please follow other related articles on the PHP Chinese website!