Example of php cross-origin json call

Source: Internet
Author: User

JSON and XML are no different, but JSON has a wider application, that is, cross-Origin data calling. Due to security issues, AJAX does not support cross-origin calls. Therefore, it is very troublesome to call data under different domain names. The following example shows how php uses json for cross-origin calls.

Index.html
Copy codeThe Code is as follows:
<Script type = "text/javascript">
Function getProfile (str ){
Var arr = str;
Document. getElementById ('Nick '). innerHTML = arr. nick;
}
</Script>
<Body> <div id = "nick"> </div> </body>
<Script type = "text/javascript" src = "http://www.openphp.cn/demo/profile.php"> </script>

Profile. php
Copy codeThe Code is as follows:
<? Php
$ Arr = array (
'Name' => 'tanteng ',
'Nick '=> 'pony ',
'Contact '=> array (
'Email '=> 'a @ gmail.com ',
'Website' => 'HTTP: // aa.sinaapp.com ',
)
);
$ Json_string = json_encode ($ arr );
Echo "getProfile ($ json_string )";
?>

When index.html calls profile. php, The JSON string is generated, and the getProfile is passed as a parameter, and the nickname is inserted into the div. This completes a cross-Origin data interaction. Is it especially simple.

Related Article

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.