1. Know Ajax (Ajax operations must run on the server, and not cross-domain, only access to other files under the same domain name) Ajax basic Format ↓↓
<script>//ajax basic format var xhr = new XMLHttpRequest ();
Xhr.open (' Get ', ' 01-test.txt ', true);
Xhr.send ();
Xhr.onreadystatechange = function () {if (xhr.readystate = 4) {alert (xhr.responsetext);
};
}; /* Note 1.
var xhr = new XMLHttpRequest (); To create an Ajax object, AJAX implementations are implemented through this object 2. Xhr.open ();
Ready to send request three parameters: first: By what way ' get ' post ' second: Access who URL Path Third: Asynchronous true false 3. Xhr.send (); No parameter 4.xhr.onreadystatechange = function () {if (xhr.readystate = 4) is required to send request get mode {Execute
function};
}; . onreadystatechange when readyState changes. ReadyState Status Code 0: The request has not yet been established (before open execution); 1: The request was established, but
is not yet sent (open just executed); 2: The request was officially sent (send executed); 3: The request has been entertained, some of the data is already available, but has not been fully processed; 4: Request complete processing completed. ResponseText The requested page returns the string data */</script> the difference between get and post methods in form forms ↓↓ 1.get
<form action= "get.php" method= "get" >
<input type= "text" placeholder= "Please enter your username" name= "user" > <br >
<input type= "text" placeholder= "Please enter your password" name= "pwd" ><br>
<input type= "Submit" >
</form>
using the Get method to submit the form, the submitted information is displayed in the URL link, and the page becomes a new page. 2.post
<form action= "post.php" method= "POST" >
<input type= "text" placeholder= "Please enter your username" name= "user" > <br >
<input type= "text" placeholder= "Please enter your password" name= "pwd" ><br>
<input type= "Submit" >
</form>
using the Post method, although the submitted information is not displayed in the URL, it still jumps to the new page. in peacetime, some pages, we want to achieve the effect is not to refresh the page does not jump the case of a piece of information on the page to update, this will use Ajax. the method of Get and post in 2.ajax and the application of 1.get↓↓
var xhr = new XMLHttpRequest ();
Xhr.open (' Get ', ' get.php?user=afei&pwd=789 ', true); When you use the Get method, you need to write the data in user=afei&pwd=789 to the URL? Back
xhr.setrequestheader (' Content-type ', ' application/ X-www-form-urlencoded ');
Xhr.send (); Get method, send () can not fill in parameter
xhr.onreadystatechange = function () {
if (xhr.readystate = = 4) {
alert ( Xhr.responsetext);
};
2.post↓↓
var xhr = new XMLHttpRequest ();
Xhr.open (' Post ', ' post.php ', true); Post method URL does not need to write data
xhr.setrequestheader (' Content-type ', ' application/x-www-form-urlencoded ');
Xhr.send (' user=afei&pwd=456 '); The Post method writes the data to be sent in send ()
xhr.onreadystatechange = function () {
if (xhr.readystate = = 4) {
alert ( Xhr.responsetext);
}
;
3.ajax application (get array, JSON data) ↓↓
SetInterval (function () {
var xhr = new XMLHttpRequest ();
Xhr.open (' Get ', ' data.txt ', true); Data.txt was a JSON file I had written in advance, which contained the data to fetch
xhr.setrequestheader (' Content-type ', ' application/x-www-form-urlencoded ' );
Xhr.send ();
Xhr.onreadystatechange = function () {
if (xhr.readystate = 4) {
var data = eval (xhr.responsetext); Eval here will get the JSON as a JS code to read into the group of
var Oul =document.queryselector (' #box ul '); Appends the obtained array to the new Ul,li
oul.innerhtml = ';
for (var i=0 i<data.length; i++) {
var oLi = document.createelement (' li ');
oli.innerhtml = ' Name: ' + Data[i].name + ', Age: ' + data[i].age;
Oul.appendchild (oLi);
}}}
;
},3000)
data.txt Content ↓↓
[{
"name": "First",
"age": "},{" "
Name": "Second", "Age
": "},{", "
name": "Third",
"" Age ":"
},{
"name": "The Fourth", "Age
": "},{" "
Name": "The Fifth",
"age": "
},{
" "Name": "Sixth",
"Age": ""
}]
Effect Chart (updated every three seconds without refreshing the page) ↓↓
as mentioned in the eval () method, he would read all the parameters passed to him as JS code, including the non-JSON part, which would be unsafe and might turn unwanted content into global variables, which would have an impact on our code. There is also a method, Json.parse (), if the incoming JSON does not turn, and in the incoming JSON, the attribute and value must be in double quotes. These two methods are compatible IE9 and above, to be compatible IE9 the following, need to download json2.js for reference. 3.ajax of encapsulation
<script>/* Xhr.status HTTP status code */Ajax ({url: ' get.php ', data: ' User=hei
Hei&pwd=111 ', Success:function (msg) {alert (msg);
Error:function (STD) {alert (' Access error, error code: ' + std ');
}
}); /* Ajax functions accept a JSON parameter, which can have properties: type:string, the way to send the request, default ' get ', url:string required, access to the file path, a Ysn:boolean optional, asynchronous, default true, Data:string/json, optional, need to send data, success:function, request a successful callback function, the first parameter represents the returned data
, the Error:function is selected, the callback function is requested, and the first parameter represents the HTTP status code. */function Ajax (Mjson) {var type = Mjson.type | |
' Get ';
var url = mjson.url; var aysn = Mjson.aysn | |
True
var data = Mjson.data;
var success = mjson.success;
var error = Mjson.error;
if (typeof data = = ' object ') {var str = ';
for (var key in data) {str + + key + ' = ' + Data[key] + ' & '}; data = str + ' _= ' + new Date (). GettimE ();
}else if (typeof data = = ' string ') {data + = ' &_= ' + new Date (). GetTime ();
} if (Data &&/^get$/i.test (type)) URL = '? ' + data;
var xhr = new XMLHttpRequest ();
Xhr.open (type, URL, aysn);
Xhr.setrequestheader (' Content-type ', ' application/x-www-form-urlencoded ');
Xhr.send (data); Xhr.onreadystatechange = function () {if (xhr.readystate = 4) {if (Xhr.status >= &&
Xhr.status <) {Success && success (Xhr.responsetext);
}else{Error && error (xhr.status); {}}}} </script>
This completes the Ajax encapsulation and, in the future, you can refer directly to the following function and then use AJAX () to pass the JSON parameter.