Using JSON to implement cross domain calls of data
background profile.php Code:
[code].lt; PHP
$arr = array. X.com',
'website' =.gt;'http://www.xxx.net',
)
]
needs to be pointed out that in non UTF-8 encoding, Chinese characters will not be encode, the result will come out of the empty value, so if you use GB2312 to write PHP code, then you need to convert the contents of the Chinese with iconv or MB to UTF-8 and then json_encode.
index.html code of the front desk:
[code].lt; script type=.quot; text/javascript.quot;.Gt;
function getProfile. Quot; firstname.quot;.Gt;.Lt; /div.gt;
.lt; /body.gt;
.lt;! T;.Lt; /script.gt; [/code]
assign the JSON data directly to the variables in JavaScript and turn into an array, and the next operation will be very convenient. If XML is used as a data transfer, the subsequent operation is inconvenient.
obviously, when index.html calls profile.php, the JSON string is generated and passed into the getProfile as a parameter, and then the nickname is inserted into the div, so that the cross domain data interaction is completed.