ajax技术

前端向后端做网络请求

如下代码:

1.创建可以做网络请求的对象
let xhr=new XMLHttpRequest()||new ActiveXObject()

2.配置连接信息

第二个参数为请求的网址

xhr.open("GET",`http://192.168.1.6:8888/ajax1?name=karen&count=20`,true)

这里会请求ajax1

3.发起网络请求

xhr.send()

4.监听网络数据

xur.onreadystatechange=function(){}

xhr.readyState==4表示网络请求成功  xhr.statue=200表示业务请求成功  xhr.statue=404表示业务请求失败

xhr.responseText表示后端返回的数据

JSON.parse()转对象

function load1(){
        console.log(666);
        // AJAX技术
        // window.XMLHttpRequest
        // 1.创建ajax对象
        let xhr=new XMLHttpRequest()||new ActiveXObject()//创建的这个对象可以做网络请求
        // 2. 配置联接信息
        xhr.open("GET",`http://192.168.1.6:8888/ajax1?name=karen&count=20`,true)
        // 3.发起网络请求
        xhr.send()
        // 4.监听网络数据
        xhr.onreadystatechange=function(){
            console.log(66666);
            console.log(xhr.readyState);//xhr.readtState一直在变化1234 等于4表示后端已经返回了数据
            if(xhr.readyState==4&&xhr.status==200){//status=200表示网络请求成功
                console.log(xhr.responseText,11);//打印后端发送的数据
            }else if(xhr.readyState==4&&xhr.status==404){
                console.log(xhr.responseText,"网络请求成功业务失败");
                var obj=JSON.parse(xhr.responseText)//转对象  
                console.log(obj);
            }
        }
        
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值