ajax常用知识点

来源:互联网 发布:apache ab post json 编辑:程序博客网 时间:2024/06/16 23:19

1.        常见协议:

(1)HTTP、HTTPS 超文本传输协议

(2)FTP文件传输协议

(3)AMTP简单邮件传输协议

2.        HTTP协议:即超文本传输协议,网站是基于HTTP协议的,例如网站的图片、CSS、JS等都是 HTTP协议进行传输的。

3.        HTTP协议是由从客户端到服务器的请求(Request)和从服务器到客户机的响应(Response)进行了约束和规范。

4.        请求:

(1)请求行:POST/01day/code/login.phpHTTP/1.1 由请求方式、请求URL和协议版本构成。

(2)请求头:Host:localhost请求的主机。POST请求有content_Type,而GET没有

(3)请求主体:POST请求有请求主体,而GET没有

5.        响应:

(1)状态行

(2)响应头

(3)响应主体

6.        AJAX(AstnchronousJavascript And XML),AJAX不是一门新的语言,而是对现有技术的综合利用。本质是在HTTP协议的基础上以异步的方式与服务器进行通信。

7.        异步:指某段程序执行时不会阻塞其它程序执行,其表现形式为程序的执行顺序不依赖程序本身的书写顺序,相反则为同步。其优势在于不阻塞程序的执行,从而提升整体执行效率。打电话是同步,发消息是异步。XMLHttpRequest可以以异步方式的处理程序。

8.        XMLHttpRequest:

(1)浏览器内置对象,用于在后台与服务器通信(交换数据),由此我们便可实现对网

页的部分更新,而不是刷新整个页面。Data、XMLHttpRequest:向服务器发起请求,

var xhr=new XMLHttpRequest();

xhr.open(‘post’,’8-1.php’);    设置了请求行

xhr.setRequestHeader(‘Content-Type’,,application/x-www-form-urlencoded’);//设置请求头

xhr.send(‘username=itcast&pass=123’);  //设置请求主体

xhr.onreadystatechange=function(){    //接收服务器响应

console.log(xhr.readyState);

if(xhr.readyState==4){   

console.log(xhr.responseText);  //响应主体

Document.getElementById(‘result’).innerHTML=xhr.responseText;

}

}

(2)get和post差异

var xhr=new XMLHttpRequest();

xhr.open(‘get’,’8-2.php’);

xhr.send(null);

xhr.onreadystatechange=function(){

if(xhr.readyState==4){

var result=xhr.responseText;

console.log(result);

}

}

(3)status:状态码

var xhr=new XMLHttpRequest();

xhr.open(‘get’,’8-31.php’);

xhr.send(null);

xhr.onreadystatechange=function(){

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

var result=xhr.responseText;

console.log(result);

}

}

(4)同步&异步

var xhr=new XMLHttpRequest;

xhr.open(‘get’,’8-4.php’,false);

xhr.send();

console.log(xhr.responseText);

console.log(‘我被执行了’);

(5)XMLHttpRequest(传参)

var xhr=new XMLHttpRequest();

xhr.open(‘get’,’8-31.php?username=itcast&pass=123’);

xhr.send(null);

xhr.onreadystatechange=function(){

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

var result=xhr.responseText;

console.log(result);

}

}

(6)XMLHttpRequest(API详解)

xhr.open()发起请求,可以是get、post方式

xhr.setRequestHeader()设置请求头

xhr.send()发送请求主体,get方式使用xhr.send(null)

xhr.onreadystatechange=function(){}监听响应状态

xhr.status表示响应码,如200

xhr.statusText 表示响应信息,如OK

xhr.getAllResponseHeaders()获取全部响应头信息

xhr.getResponseHeader(‘key’)获取指定头信息

xhr.responseText,xhr.responseXML都表示响应主体

9.        面试题

(1)GET没有请求主体,使用xhr.send(null)

(2)GET可以通过在请求URL上添加请求参数

(3)POST可以通过xhr.send(‘name=itcast&age=10’)

(4)POST需要设置xhr.setRequestHeader(‘Content-Type’,’application/x-www-form-urlencoded’);

(5)GET效率更好(应用多)

(6)GET大小限制约4K,POST则没有限制

原创粉丝点击