ajax的get和post请求

(1)get请求没有请求头,post请求要有请求头,请求会带上content—type告诉服务器post过去的数据格式和url的参数的数据一样
(2)post发送的数据在请求体中,用户看不到;get发送的数据在地址栏中,用户能看到,不安全
(3)涉及隐私数据的时候用 post 请求比较安全,发送大量数据的时候用 post,发送少量数据用get

1.get请求
(1) 创建

var xhr = null 
	if(window.XMLHttpRequest){
		xhr = new XMLHttpRequest()
	}else{
		xhr = new ActiveOObject("Microsoft.XMLHTTP")
	}

(2)绑定监听事件

xhr.onreadyStatechange = function(){
	if(xhr.readyState == 4){
			if(xhr.status == 200){
				...
		}
	}
}

(3) 建立连接

xhr.open("GET","04.php?username=" + uname,true);
//04.php为地址 和地址栏的格式保持一致

(4) 发送请求

xhr.send(null)   //get请求的参数都已经到地址栏中,所以这里可以发送空

2.post请求
(1) 创建

var xhr = null 
	if(window.XMLHttpRequest){
		xhr = new XMLHttpRequest()
	}else{
		xhr = new ActiveOObject("Microsoft.XMLHTTP")
	}

(2)绑定监听事件

xhr.onreadyStatechange = function(){
	if(xhr.readyState == 4){
			if(xhr.status == 200){
				...
		}
	}
}
```POST
(3) 建立连接

```javascript
xhr.open("POST","04.php",true);
//04.php为地址 和地址栏的格式保持一致

在第三步和第四步之间的必须的一步

xhr.setRequestHeader('content-type', "application/x-www-form-urlencoded") //post请求发送之前,必须设定发送的格式

(4) 发送请求

xhr.send()  //  括号内容为要传输的参数    post请求的参数放在send中   

jquery的请求
(1)get

$.ajax({
                    type: "GET",
                    url: "02.php?username=" + $('#username').val(),
                    success: function(data) {
                        console.log(data)
                    },
                    error: function(xhr) {
                        // console.log(xhr.status)
                    }
                })

(2)

 $.ajax({
                    type: "POST",
                    url: "03.php",
                    data: {
                        stuName: $("#username").val(),
                        score: $("#userscore").val()
                    },
                    success: function(data) {
                        console.log(data)
                    },
                    error: function(xhr) {
                        console.log(xhr)
                    }
                })
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值