AJAX interview succinctly

  1. ajax concept:

Asynchronous javascript and xml, AJAX is a technique used to create dynamic web pages quickly. ajax is used to interact with the background.

  1. What is cross-domain ajax?

Origin policy restrictions will result in different sources across domains.

  1. How to solve the problem of cross-domain ajax?

  1. , Jsonp can only be resolved get across domains.

Principle: to create a dynamic script tag. Using the src attribute of the script tag is not the same origin policy restrictions. Because all?

1, to create a script tag;

2, script src attribute interface addresses;

3, interface parameters must be with a custom function name can not be returned or else the background data;

4, by defining a function name to receive return data back.

  1. , CORS cross-domain resource sharing.

How it works: After the server set up Access-Control-Allow-OriginHTTP response header, the browser will allow cross-domain please ??

Restrictions: browser does not support HTML5, supports POST, PUT and other methods more compatible ie9

  1. Set document.domain

Principle: page under the same subdomain different main domain, so that they can set the document.domain sympatric

Restrictions: Domain document are provided with interoperability between pages, pages need to load iframe

  1. Ajax request step is what?

Step 1: Create an asynchronous objects

var xhr = new XMLHttpRequest();

Step Two: Set request line open (mode request, a request URL):

// get the request parameter if you need to splice parameters behind the url,

// post if there are parameters in the body xhr.open transfer request ( "get", "validate.php? Username =" + name)

xhr.open("post","validate.php");

Step 3: Set request (GET ignoring this step) header: the setRequestHeader ()

// 1.get not need to set

// 2.post need to set the request header: Content-Type: application / x-www-form-urlencoded

xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");

第四步:设置请求体 send()

// 1.get的参数在url拼接了,所以不需要在这个函数中设置

// 2.post的参数在这个函数中设置(如果有参数)

xhr.send(null) xhr.send("username="+name);

第五步:让异步对象接收服务器的响应数据

// 一个成功的响应有两个条件:1.服务器成功响应了 2.异步对象的响应状态为4(数据解析完毕可以使用了)

xhr.onreadystatechange = function(){

if(xhr.status == 200 && xhr.readyState == 4){

  console.log(xhr.responseText);

   }

  1. 常见状态码?

1:400 (错误请求) 服务器不理解请求的语法。

2:403 (禁止) 服务器拒绝请求。

3:404 (未找到) 服务器找不到请求的网页。

4:500 (服务器内部错误) 服务器遇到错误,无法完成请求。

5:501 (尚未实施) 服务器不具备完成请求的功能。 例如,服务器无法识别请求方法时可能会返回此代码。

6:502 (错误网关) 服务器作为网关或代理,从上游服务器收到无效响应。

7:503 (服务不可用) 服务器目前无法使用(由于超载或停机维护)。 通常,这只是暂时状态。

8:504 (网关超时) 服务器作为网关或代理,但是没有及时从上游服务器收到请求。:

9:505 (HTTP 版本不受支持) 服务器不支持请求中所用的 HTTP 协议版本。

  1. json解析的方式?

对于简单的json字符串或者数组数据,可以用:JsonObject和JsonArray。

使用以上两种方式解析json均需要依赖json-lib.jar开发包使用依赖包。

1、如果只是一条简单的json数据,可以直接用JsonObject即可

2、如果是数组数据的json,用JsonArray比较合适

3、对于复杂的json数据,需要借助jackson框架,先泛型转化,再readValue

 

发布了400 篇原创文章 · 获赞 940 · 访问量 49万+

Guess you like

Origin blog.csdn.net/A_BlackMoon/article/details/102828014