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

coldplay.xixi
Release: 2023-04-09 12:54:01
forward
2402 people have browsed it

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) { 
// 获得返回值 
} 
}); 
}
Copy after login

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]; 
} 
?>
Copy after login

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).")"; 
} 
?>
Copy after login

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!

Related labels:
source:jb51.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template