Home >Backend Development >PHP Tutorial >Ajax cross-domain call implementation code using jQuery in PHP_PHP tutorial

Ajax cross-domain call implementation code using jQuery in PHP_PHP tutorial

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-07-21 15:20:09814browse

You can define a calling method on the page, as follows:

Copy code The code is as follows:

function getData(){
$.getJSON("http://123.123.123.123/?callback=?",
{
"m":"data",// Specify the php file name
"act":"getdata ", // Specify the method in the php file
"name": "Problem Child" // Incoming parameters
},
function(data) {
// Get the return value
}
});
}

The PHP file corresponding to the link (123.123.123.123) generally calls the index.php file first by default, through the method in the index.php file After processing, go to the corresponding php file, find the corresponding method, and execute it.
index.php code is as follows:
Copy code Code is as follows:

/* *
* Entry file
*/
$string = $_SERVER["REQUEST_URI"];// Get the accessed url
$m = get_m($string);
$file_path = "app/".$ m.".php";
define('IS_INDEX',true);// Prevent direct access to the app directory
require ($file_path);
/**
*
* 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:
Copy code The code is as follows:

/**
* 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 can obtain the returned json data.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/325148.htmlTechArticleYou can define a calling method on the page, as follows: Copy the code as follows: function getData(){ $.getJSON( "http://123.123.123.123/?callback=?", { "m":"data",//Specify the file name of php...
Statement:
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