PHP USES jQuery's Ajax cross domain call implementation code

  • 2020-05-12 02:25:20
  • OfStack

You can define 1 calling method on the page, as follows:
 
function getData(){ 
$.getJSON("http://123.123.123.123/?callback=?", 
{ 
"m":"data",//  The specified php File name of  
"act":"getdata",//  The specified php Method in file  
"name":" Problem child "//  Incoming parameter  
}, 
function(data) { 
//  Get the return value  
} 
}); 
} 

For the PHP file under the corresponding link (123.123.123.123), 1 first calls the index.php file by default. After processing through the method in the index.php file, it goes to the corresponding php file, finds the corresponding method, and executes it.
index. php code is as follows:
 
<?php 
/** 
*  Entrance to the file  
*/ 
$string = $_SERVER["REQUEST_URI"];//  Get access to url 
$m = get_m($string); 
$file_path = "app/".$m.".php"; 
define('IS_INDEX',true);//  Block direct access app directory  
require ($file_path); 
/** 
* 
*  To get access to php file  
* @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:
 
<?php 
/** 
* data file  
*/ 
$act = !empty($_GET['act']) ? $_GET['act'] : ''; 
if ($act == 'getdata') 
{ 
$name = " My name is: ".$_REQUEST['name']; 
echo $_REQUEST["callback"]."(".json_encode($name).")"; 
} 
?> 

After a successful call, the screen gets the returned json data.

Related articles: