PHP passes an array of JSON objects passed through Ajax to the foreground, foreground parsing, and traversing JSON

Source: Internet
Author: User

PHP Backstage Connect NetEase interface

Header ("Content-type:text/html;charset=utf-8");
Reading data from a file
$json _str =file_get_contents ("Wangyidata.json");
$str =file_get_contents ("http://c.m.163.com/nc/article/list/T1348647853363/0-20.html");
Echo $str;


Via Ajax to the foreground, package native JS
1.type request mode post, or get
2. Request URL Address
3. Parameters (Object)
4. Callback function
5. Synchronous or asynchronous
Request Way Request URL Address parameter: is an object callback function synchronous asynchronous
function Ajax (Type,url,par,fn,async) {
Determine if the request is legal
Because incoming may have uppercase or lowercase, unified conversion to uppercase, easy to verify
Type =type.touppercase ();
Judging legality
if (Type! = "GET" && type! = "POST") {
Console.error ("Illegal request Method");
Return
}
var ajaxobj;
Determine browser type, create Ajax object
if (window. XMLHttpRequest) {
Ajaxobj =new XMLHttpRequest ();
}else {
Ajaxobj =new ActiveXObject ("Microsoft.XMLHTTP");
}
Processing parameters
var data = "";
For (Var prop in par) {
Data + = prop + "=" +par[prop]+ "&";
}
Data =data.substr (0,DATA.LENGTH-1);
if (type = = "GET") {
URL + = "?" +data;
}
Call the Open
Ajaxobj.open (Type,url,async);
if (type = = "POST") {
Ajaxobj.setrequestheader ("Content-type", "application/x-www-form-urlencoded");
Ajaxobj.send (data);
}else{
Get
Ajaxobj.send ();
}
Listen for data return, notify user asynchronously via callback function
Ajaxobj.onreadystatechange =function () {
if (4 ==ajaxobj.readystate &&200 ==ajaxobj.status) {
FN (Ajaxobj.responsetext);
}
}
}

Foreground JS parsing JSON traversal JSON object array
var data = Json.parse (res);

for (var i in data) {
for (Var j in Data[i]) {
Data. I [j] ["XXXX"];
}

Another way to traverse

var data = Json.parse (res);
var Headarr = data["T1348647853363"];


For (var i= 2; i < headarr.length; i++) {

Console.log (headarr[i]["xxxx"]);

}

PHP passes an array of JSON objects passed through Ajax to the foreground, foreground parsing, and traversing JSON

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.