Php implements jquery + ajax cross-origin data calling instances. This article is based on jquery ajax to implement cross-origin data calling. it mainly uses php to return json data in real time, which facilitates ajax cross-origin data calling. Jquery file I am not here this article is based on jquery ajax to implement cross-origin data calling, mainly to use php to return json data in real time, so that ajax cross-origin data calling is convenient.
I don't want to download the jquery file here. you can download it directly from google,
You can define a call method on the page as follows:
| The code is as follows: |
|
|
Function getData (){
$. GetJSON ("http: // 123.123.123.123 /? Callback =? ",
{
"M": "data", // specify the php file name
"Act": "getdata", // specifies the method in the PHP file
"Name": "Problem Child" // input parameters
},
Function (data ){
// Obtain the returned value
}
});
}
|
The corresponding php file (123.123.123.123) under the link. generally, index is called first by default. php file, through index. after processing the methods in the 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: |
|
|
/**
* Portal file
*/
$ String = $ _ SERVER ["REQUEST_URI"]; // Obtain the access url
$ M = get_m ($ string );
$ File_path = "app/". $ m. ". php ";
Define ('is _ INDEX', true); // block direct access to the app directory
Require ($ file_path );
/**
*
* Obtain the php file for access
* @ Param string $ url
*/
Function get_m ($ url ){
$ Strings = explode ('m =', $ url );
$ Res = explode ("&", $ strings [1]);
Return empty ($ res [0])? 'Index': $ res [0];
}
?>
|
The data. php code is as follows:
| The code is as follows: |
|
|
/**
* Data File
*/
$ Act =! Empty ($ _ GET ['AC'])? $ _ GET ['AC']: '';
If ($ act = 'getdata ')
{
$ Name = "My name is:". $ _ REQUEST ['name'];
Echo $ _ REQUEST ["callback"]. "(". json_encode ($ name ).")";
}
?> |
After successful call, the screen will be able to get the returned json data.
Summary:
The simple method is to 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 implements the cross-origin data call.
Bytes. Jquery file I am not here...