Home >
front end > Beginners small white consult, using ajax after encapsulation, PHP has return data, but the console.
Beginners small white consult, using ajax after encapsulation, PHP has return data, but the console.
<meta charset="utf-8" & gt;
Ajax encapsulation: the object and packaging & lt;/title>
<body>
<script>
Var get=document. GetElementById (" get ");
Var post=document. GetElementById (" post ");
Get the onclick=function () {
$. Ajax ({
Type: "get",
Url: "ajaxfz/get. PHP,"
Data: {
Username: "1515",
Password: "4155451"
},
Success: the function (info) {
console.log(info);
}
})
};
Post. The onclick=function () {
$. Ajax ({
Type: "POST",
Url: "ajaxfz/post1. PHP,"
DataType: "json",
Parmas: "username=13 & amp; Password=54321 ",
Success: the function (info) {
console.log(info);
},
Error: function () {
Fried the console. The log (" server ");
}
})
};
/* parameter analysis
Type: get | post, the default value: get
Url: must pass, get requests need the stitching in the address bar of the parameter
Async: whether asynchronous, default true
Data: the parameters of the request, is an object that needs to be converted to string format (example: the username=12 & amp; Password=54321)
Params: the parameters of the request
DataType: response to the data type of the XML/json/text
Success: the success of the () callback function
Error: failed callback function
*/
Var $={
Ajax: the function (options) {//options is an object
//if the params not preach object or of a parameter, directly back to
if (! The options | | typeof options! {
="object")return;
}
//if the type is a post by post processing, if not press the get processing, defaults to get
Var type=options. Type==="POST"? "POST" : "get";
Var url=options. The url;
if (! Url) {
return;
};
//if the transfer is not false, then the true, the default is true
Var async=options. Async===false? False: true,
Var data=https://bbs.csdn.net/topics/options.data;
//the parse converts data into params string
//parameters passed
Var params=$. Parse (data);
//response data type
Var dataType=options. DataType;
After the success of the//to do
Var success=options. Success;
//after failing to do
Var error=options. The error;
Var XHR=new XMLHttpRequest ();
//1. Request line, get requests need to be in the address bar splicing parameter
If (type==="get") {
Url=url + "?" + params.
Params=null;//if the parameter has been spliced in the address bar, parameter is set to null
}
XHR. Open (type, url, async);
//2. Request header, a post request need to set the request header
If (type==="POST") {
XHR. SetRequestHeader (" the content-type ", "application/x - WWW - form - urlencoded");
}
//3. Psot request required parameters, get requests pass null (null)
XHR. Send (params);
//add to monitor, process the response
XHR. Onreadystatechange=function () {
If (XHR. ReadyState===4) {
If (XHR. Status===200) {
//response success
//if success exists (true) implementation success (), there is no (false) does not perform success ()
//according to different types of response, you need to do different data processing XML/json/text
Var result=null;
If (dataType==="XML") {
Result=XHR. ResponseXML;
} else if (dataType==="json") {
Result=JSON. Parse (XHR. The responseText);
//the background response back text into a json array or object
} else if (dataType==="text") {
Result=XHR. The responseText;
}
Success & amp; & Success (result);
} else {
//response to the above failure
The error & amp; & Error (XHR. The responseText);
}
}
}
},
Parse: function (obj) {
//function: converting an object into (username=12 & amp; Password=54321) string
if (! Obj | | typeof obj! {
="object")return null;
}
Var arr=[];
//object traversal
For (var k in obj) {
Arr. Push (k + "=" + obj [k]);//k as key, obj [k] for value
}
Return arr. Join (& amp; "" );//insert between string & amp;
}
}
</script>