Ajax common face question 1: What is AJAX? What is the role of Ajax?
异步的javascript和xml AJAX 是一种用于创建快速动态网页的技术。 ajax用来与后台交互
2: How many steps does native JS Ajax request have? What's the difference?
//创建 XMLHttpRequest 对象var ajax = new XMLHttpRequest();//规定请求的类型、URL 以及是否异步处理请求。ajax.open(‘GET‘,url,true);//发送信息至服务器时内容编码类型ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); //发送请求ajax.send(null); //接受服务器响应数据ajax.onreadystatechange = function () { if (obj.readyState == 4 && (obj.status == 200 || obj.status == 304)) { }};
3:json string Conversion Set JSON object, JSON object conversion JSON string
//字符串转对象JSON.parse(json)eval(‘(‘ + jsonstr + ‘)‘) // 对象转字符串JSON.stringify(json)
4:ajax several request methods? Their pros and cons?
A common post,get,delete. Do not use copy, head, link and so on.
###代码上的区别 1:get通过url传递参数 2:post设置请求头 规定请求数据类型###使用上的区别 1:post比get安全 (因为post参数在请求体中。get参数在url上面) 2:get传输速度比post快 根据传参决定的。 (post通过请求体传参,后台通过数据流接收。速度稍微慢一些。而get通过url传参可以直接获取) 3:post传输文件大理论没有限制 get传输文件小大概7-8k ie4k左右 4:get获取数据post上传数据 (上传的数据比较多 而且上传数据都是重要数据。所以不论在安全性还是数据量级 post是最好的选择)
5: What situations cause cross-domain?
The same-origin policy restricts different sources from causing cross-domain. Any of the following are different sources.
Http://www.baidu.com/8080/index.html
/
http |
different Protocols |
Www |
Sub domain names are different |
Baidu.com |
Primary domain name is different |
8080 |
Different port numbers |
Www.baidu.com |
IP address and URL are different |
6: What are the cross-domain solutions? 1:JSONP can only resolve get cross-domain (maximum number of questions)
- Principle: Dynamically create a script tag. The SRC attribute of the script tag is not restricted by the same-origin policy. Because all src attributes and href attributes are not restricted by the same-origin policy. You can request third-party server data content.
- Steps:
- To create a script tag
- The SRC attribute of the script sets the interface address
- Interface parameter, you must take a custom function name or the background cannot return data.
- Receive background return data by defining function name
//去创建一个script标签var script = document.createElement("script");//script的src属性设置接口地址 并带一个callback回调函数名称script.src = "http://127.0.0.1:8888/index.php?callback=jsonpCallback";//插入到页面document.head.appendChild(script);//通过定义函数名去接收后台返回数据function jsonpCallback(data){ //注意 jsonp返回的数据是json对象可以直接使用 //ajax 取得数据是json字符串需要转换成json对象才可以使用。}
2:cors: Cross-domain resource sharing
- Rationale: After the server sets the Access-control-allow-originhttp response header, the browser will allow cross-domain requests
- Restrictions: Browsers need to support HTML5, can support Post,put and other methods compatible with IE9 above
需要后台设置Access-Control-Allow-Origin: * //允许所有域名访问,或者Access-Control-Allow-Origin: http://a.com //只允许所有域名访问
3: Set Document.domain
- Principle: The same primary domain name under different sub-domain pages, you can set document.domain let them the same domain
- Limitations: The same domain document provides interoperability between pages and needs to be loaded into the IFRAME page
// URL http://a.com/foovar 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);
4: Using Apache to do forwarding (reverse proxy), so that cross-domain into the same domain 7:http common status code? One: 2 opening status code
2xx (Success) indicates the status code of the request was successfully processed
200 (成功) 服务器已成功处理了请求。 通常。
Two: 3 Opening status codes
3xx (redirect) indicates that further action is required to complete the request. Typically, these status codes are used for redirection.
304 (未修改) 自从上次请求后,请求的网页未修改过。 服务器返回此响应时,不会返回网页内容。
Three: 4 opening status code
4xx (Request error) These status codes indicate a possible error in the request and prevent the server from processing
1:400 (错误请求) 服务器不理解请求的语法。 2:403 (禁止) 服务器拒绝请求。3:404 (未找到) 服务器找不到请求的网页。
Four: 5 opening status code
5xx (server error) These status codes indicate that the server has an internal error while trying to process the request. These errors may be the error of the server itself, not the request
error
500 (服务器内部错误) 服务器遇到错误,无法完成请求。501 (尚未实施) 服务器不具备完成请求的功能。 例如,服务器无法识别请求方法时可能会返回此代码。502 (错误网关) 服务器作为网关或代理,从上游服务器收到无效响应。503 (服务不可用) 服务器目前无法使用(由于超载或停机维护)。 通常,这只是暂时状态。504 (网关超时) 服务器作为网关或代理,但是没有及时从上游服务器收到请求。505 (HTTP 版本不受支持) 服务器不支持请求中所用的 HTTP 协议版本。
Reprint: https://juejin.im/post/5aa2b26b518825556020873f
Programmers who don't like to write code
Source: Denver Nuggets
Copyright belongs to the author. Commercial reprint please contact the author for authorization, non-commercial reprint please specify the source.
What are the common HTTP status codes?