原生js的ajax请求

//针对get方法
var xhr = new XMLHttpRequest();
xhr.open("get",'getStar.php?starName='+name,true);
xhr.send();
xhr.onreadyStateChange=function () {
    if(readyState==4 && status==200){
      console.log(xhr.responseText);
    }
};
//针对post方法
var xhr = new XMLHttpRequest();
xhr.open("post","02.post.php",true);
xhr.setRequestHeader("content-type","application/x-www-form-urlencoded");
xhr.send("name=123");
xhr.onreadyStateChange=function () {
  if(readyState==4 && status==200){
    console.log(xhr.responseText);
  }
};

注意:
1.setRequestHeader()把指定首部设置为所提供的值。在设置任何首部之前必须先调用open()。设置header并和请求一起发送 (‘post’方法一定要 )
2.post请求一定要添加请求头才行不然会报错
3.open("method","URL",[asyncFlag],["userName"],["password"]) 建立对服务器的调用。method参数可以是GET、POST或PUT。url参数可以是相对URL或绝对URL。这个方法还包括3个可选的参数,是否异步,用户名,密码
4.send(content) 向服务器发送请求
5.需要兼容IE,单独书写代码

参考:http://www.cnblogs.com/cythia/p/6978323.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值