Php implementation of jquery + ajax cross-origin data call instance _ PHP Tutorial

Source: Internet
Author: User
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...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.