1: What is Ajax? What does Ajax do?
Asynchronous javascript and XML AJAX is a technique for creating fast, dynamic web pages. Ajax is used to interact with the backgroundCopy the code
How many steps are there in a native JS Ajax request? What are they
Var ajax = new XMLHttpRequest(); // Specifies the type of request, the URL, and whether to process the request asynchronously. ajax.open('GET',url,true); // The content encoding type when sending messages to the server ajax. SetRequestHeader ("Content-type"."application/x-www-form-urlencoded"); // Send the request ajax.send(null); // Accept the server response data ajax.onReadyStatechange =function () {
if (obj.readyState == 4 && (obj.status == 200 || obj.status == 304)) {
}
};
Copy the code
3: Converts a JSON object into a JSON string and converts a JSON object into a JSON string
// String to object json.parse (JSON)eval('(' + jsonstr + ') ') // Object to string json.stringify (JSON)Copy the code
4: How many ways to request Ajax? What are their strengths and weaknesses?
Common post, GET,delete. Don’t use copy, head, link, etc.
### difference in code1: GET passes the parameter through the URL. 2: POST sets the request header and specifies the request data type### difference in use1: POST is safer than GET (because the POST parameter is in the request body). The get parameter is above the URL.) 2: The transfer speed of GET is faster than that of POST depending on the pass parameter. Post passes the parameters through the request body, and the background receives the parameters through the data stream. It's a little bit slower. Get can be directly obtained through url passarguments.) 3: Post transfer file size theory is not limited get transfer file size is about 7-8K IE4K 4: GET obtain data POST upload data (upload data is more and upload data is important data. So POST is the best choice for both security and data.)Copy the code
5: What causes cross-domain?
The same origin policy restricts different sources. Each of the following situations is a different source.
http://www.baidu.com/8080/index.html
http:// | Agreement is different |
---|---|
www | Different subdomain names |
baidu.com | Different primary domain names |
8080 | Different port numbers |
www.baidu.com | The IP address is different from the web address |
6: What are the cross-domain solutions?
1: JSONP can only solve get cross-domain (most asked)
-
How it works: Create a script tag dynamically. The SRC attribute of a script tag is not subject to the same origin policy. All SRC and href attributes are not subject to the same origin policy. Third-party server data content can be requested.
-
Steps:
- To create a script tag
- The SRC attribute of script sets the interface address
- Interface parameter, must have a custom function name or the background will not return data.
- The function name is defined to receive background return data
Var script = document.createElement();"script"); // The SRC property of script sets the interface address with a callback function named script.src ="Http://127.0.0.1:8888/index.php? callback=jsonpCallback"; / / is inserted into the page document. The head. The appendChild (script). // Define the name of the function to receive the data returned by the backgroundfunctionJsonpCallback (data){// Note that the data returned by jsonp is a JSON object that can be used directly. // The data returned by Jsonp is a JSON string that needs to be converted into a JSON object before it can be used. }Copy the code
2:CORS: cross-domain resource sharing
-
How it works: After the server sets the access-control-allow-OriginHTTP response header, browsers will Allow cross-domain requests
-
Limitations: Browsers need to support HTML5, can support POST, PUT and other methods compatible with IE9 or above
Access-control-allow-origin: * // Allow all domain names to Access, or access-control-allow-origin: http://a.com // Only Allow all domain names to AccessCopy the code
3: set up the document. The domain
-
How it works: For pages with the same primary domain but different subdomains, you can set document.domain to make them belong to the same domain
-
Limitations: A co-domain Document provides interoperability between pages, requiring iframe pages to be loaded
// URL http://a.com/foo
var ifr = document.createElement('iframe');
ifr.src = 'http://b.a.com/bar';
ifr.onload = function(){
var ifrdoc = ifr.contentDocument || ifr.contentWindow.document;
ifrdoc.getElementsById("foo").innerHTML);
};
ifr.style.display = 'none';
document.body.appendChild(ifr);
Copy the code
4: Use Apache to do forwarding (reverse proxy), so that cross-domain into the same domain
7: What are the common HTTP status codes?
1: Status code starting with 2
2XX (success) indicates the status code that successfully processed the request
200 (Successful) The server successfully processed the request. Usually.Copy the code
Two: status code starting with 3
3XX (redirection) indicates that further action is required to complete the request. Typically, these status codes are used for redirection.
304 (unmodified) The requested page has not been modified since the last request. When the server returns this response, the web page content is not returned.Copy the code
3: Status code starting with 4
4XX (Request error) These status codes indicate that the request may be in error, hindering the server’s processing
1:400 (Error request) The server does not understand the request syntax. 2:403 (Disabled) The server rejected the request. 3:404 (not found) The server could not find the requested page.Copy the code
4: Status code starting with 5
5XX (Server Error) These status codes indicate an internal error occurred while the server was trying to process the request. These errors may be the fault of the server itself, rather than the request
500 (server internal error) The server encountered an error and was unable to complete the request. 501 (not implemented) The server does not have the capability to complete the request. For example, the server might return this code when it cannot recognize the request method. 502 (Error gateway) The server acting as a gateway or proxy received an invalid response from the upstream server. 503 (Service unavailable) The server is currently unavailable (due to overloading or down for maintenance). Usually, this is a temporary condition. 504 (Gateway timeout) The server acts as a gateway or proxy, but does not receive the request in time from the upstream server. 505 (HTTP version not supported) The server does not support the HTTP protocol version used in the request.Copy the code