Home  >  Article  >  Backend Development  >  Detailed explanation of Ajax cross-domain calling code using jQuery in PHP

Detailed explanation of Ajax cross-domain calling code using jQuery in PHP

coldplay.xixi
coldplay.xixiforward
2020-08-06 15:50:012341browse

Detailed explanation of Ajax cross-domain calling code using jQuery in PHP

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(&#39;IS_INDEX&#39;,true);// 阻止直接访问app目录 
require ($file_path); 
/** 
* 
* 获取访问php文件 
* @param string $url 
*/ 
function get_m($url){ 
$strings = explode(&#39;m=&#39;, $url); 
$res = explode("&", $strings[1]); 
return empty($res[0])?&#39;index&#39;:$res[0]; 
} 
?>

data.php code is as follows:

The code is as follows:

<?php 
/** 
* data文件 
*/ 
$act = !empty($_GET[&#39;act&#39;]) ? $_GET[&#39;act&#39;] : &#39;&#39;; 
if ($act == &#39;getdata&#39;) 
{ 
$name = "我的名字叫:".$_REQUEST[&#39;name&#39;]; 
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!

Statement:
This article is reproduced at:jb51.net. If there is any infringement, please contact admin@php.cn delete