Cross-domain with JSON for data exchange

Source: Internet
Author: User

Not long ago suddenly thought, if my data in the background, then how to put the data, to the front desk, processing, front and back not a domain, then how the data cross-domain, I think of the JSON operation. The code behind the scenes is as follows:

ht.php

$ar =array (' a ' + = ' Hello ', ' b ' = ' they ', ' d ' = ' China '); $json _str=json_encode ($ar); echo "GetProfile ($json _str)";

About JSON I would also like to say that in PHP, the data is generally converted to JSON data, using the function Json_encode ();

It should be pointed out that under the non-UTF-8 encoding, the Chinese characters will not be encode, the result will be null, so if you use gb2312 to write PHP code, then you need to use the content of the language containing iconv or MB to UTF-8 and then Json_encode.

Front page:

<! DOCTYPE html>

The JSON format of the data directly assigned to the variables in JavaScript, it becomes an array, the next operation will be very convenient, here if the use of XML as data transmission, the subsequent operation is not convenient.

Obviously, when Index.html calls Profile.php, the JSON string is generated and passed in as a parameter to the GetProfile, and the nickname is inserted into the div so that the cross-domain data interaction is complete.



Cross-domain with JSON for data exchange

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.