PHP implements jquery+ajax cross-domain data call example_PHP tutorial

WBOY
Release: 2016-07-13 16:56:07
Original
867 people have browsed it

This article is based on jquery's ajax to implement cross-domain data calls. It mainly uses php to return json data in real time, so that it is convenient to implement ajax cross-domain data calls.

I do not provide downloads of jquery files here. You can download them directly from Google.

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 code


function getData(){

$.getJSON("http://123.123.123.123/?callback=?",

{
 代码如下 复制代码

/**
* 入口文件
*/
$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];
}
?>

 

"m":"data",//Specify the file name of php

"act":"getdata",//Specify the method in the php file

"name":"Problem Child"//Incoming parameters
 代码如下 复制代码

/**
* data文件
*/
$act = !empty($_GET['act']) ? $_GET['act'] : '';
if ($act == 'getdata')
{
$name = "我的名字叫:".$_REQUEST['name'];
echo $_REQUEST["callback"]."(".json_encode($name).")";
}
?>

},

function(data) {

// Get the return value

} }); }

The PHP file under the corresponding link (123.123.123.123) generally calls the index.php file first by default. After processing it 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 Copy code
/** * 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 files <🎜> * @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 Copy code
/**<🎜> * data file <🎜>*/ <🎜> $act = !empty($_GET['act']) ? $_GET['act'] : ''; <🎜> if ($act == 'getdata') <🎜> { <🎜> $name = "My name is:".$_REQUEST['name']; <🎜> echo $_REQUEST["callback"]."(".json_encode($name).")"; <🎜> } <🎜> ?> After successful call, the screen will be able to obtain the returned json data Summary: The method is very simple. I use the json_encode() function of PHP to process the data submitted by the user and then output the json data. jquery accepts the data and realizes the cross-domain data call we want. http://www.bkjia.com/PHPjc/631624.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/631624.htmlTechArticleThis article is based on jquery's ajax to implement cross-domain data calls, mainly using php to return json data in real time, so It is convenient to implement ajax cross-domain data call. I don’t have the jquery file here...
source:php.cn
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