初试Ajax

1.代码展示及说明

html部分

<body>
    <button>点击发送请求</button>
    <div id="result"></div>
    <script>
        //获取button元素
        const btn = document.getElementsByTagName('button')[0];
        var result = document.querySelector('#result');
        //绑定事件
        btn.onclick = function(){
            //1.创建对象
            const xhr = new XMLHttpRequest();
            //2.初始化 设置请求方法 和 url
            xhr.open('GET','http://127.0.0.1:8000/jsonp-server');
            //3.发送
            xhr.send();
            //4.绑定事件 处理服务端返回结果
            xhr.onreadystatechange = function(){
                //判断(服务器返回了所有结果)
                if(xhr.readyState ===4){
                    //判断响应状态码 200 404 403 401 500
                    //2xx 成功
                    if(xhr.status >= 200 && xhr.status <300){
                        //处理结果 行 头 空行 体 
                        //1.相应行
                        // console.log(xhr.status);//状态码
                        // console.log(xhr.statusText);//状态字符串
                        // console.log(xhr.getAllResponseHeaders());//所有响应头
                        // console.log(xhr.response);//响应体
                        result.innerHTML = xhr.response;
                    }else{

                    }
                }
            }
        }
    </script>
</body>

espress部分

/引入express
const express = require('express');

//创建应用对像
const app = express();

//创建路由规则
//request 是对请求报文的封装
//response 是对响应报文的封装
app.get('/server',(request,response)=>{
    //设置响应头 设置允许跨域 
     response.setHeader('Access-Control-Allow-Origin','*');
    //设置响应体
    response.send('HELLO Ajax');
});

app.all('/jsonp-server',(request,response)=>{
    response.setHeader('Access-Control-Allow-Origin','*');
    response.send('console.log("hello jsonp")')
})

//监听端口启动服务
app.listen(8000,()=>{
    console.log("服务已启动,8000 端口监听中...");
})

2.发送Ajax请求

(1) jQ

$ajax({
    //url 
    url:'http://127.0.0.1:8000/',
    //参数
    data:{a:100 , b:200},
    //请求类型
    type: 'GET',
    //成功反馈
    success: function(date){
        console.log(date);
    }
})

(2)普通请求

element.事件类型 = function(){
    //创建对象
    x = new XMLHttpRequest();
    //设置请求类型 和 url
    x.open('GET','http://127.0.0.1:8000/');
    //发送
    x.send();
    //绑定事件 处理服务端返回结果
    x.onreadystatechange = function(){
                //判断(服务器返回了所有结果)
                if(x.readyState ===4){
                    //判断响应状态码 200 404 403 401 500
                    //2xx 成功
                    if(x.status >= 200 && x.status <300){
                        //处理结果 行 头 空行 体 
                        //1.相应行
                        // console.log(xhr.status);//状态码
                        // console.log(xhr.statusText);//状态字符串
                        // console.log(xhr.getAllResponseHeaders());//所有响应头
                        // console.log(xhr.response);//响应体
                        result.innerHTML = xhr.response;
                    }else{

                    }
                }
            }
}

(3)fetch

element.事件类型.function(){
    fetch('http://127.0.0.1:8000/',{
    //请求方法
    method:'POST',
    //请求头
    headers:{
    name:'atguihu'
    },
    //请求体
    body:'*************'

}).then(respose =>{
    return respose.text();
    }).then(respose =>{
    console.log('respose');
    });
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值