尚硅谷-AJAX学习笔记

1.AJAX请求的基本注意事项

1.1打开文件注意事项

打开文件注意事项,需要在文件所在目录下右键

<在集成终端打开>

 之后输入

node +文件名称

1.2  AJAX操作基本步骤

1.2.1 创建对象

const xhr = new XMLHttpRequest();

xhr = 对AJAX请求进行筛选

1.2.2 初始化,设置请求方法和url

xhr.open('GET', 'http://127.0.0.1:8000/server');

xhr.open('请求类型', '请求对象');

1.2.3  发送

xhr.send();

1.2.4  事件绑定,处理服务端返回的结果

 xhr.onreadystatechange = function() {
        //判断服务端返回的所有结果
                    if (xhr.readyState === 4) {
                        if (xhr.status >= 200 && xhr.status < 300) {
                            // 响应行
                            console.log(xhr.status); //状态码
                            console.log(xhr.statusText); //状态字符串
                            console.log(xhr.getAllResponseHeaders()); //所有响应头
                            console.log(xhr.response); //响应体
                        } else {

                        }
                    }
                }

2. GET地址传参

(用问好分割,在后面设置)

xhr.open('GET', 'http://127.0.0.1:8000/server?a=100&b=200&c=300');

3. 发送POST请求

POST.html

<body>
    <div id="result"></div>
    <script>
        const result = document.getElementById("result");
        result.addEventListener("mouseover", function() {
            // 1 创建对象
            const xhr = new XMLHttpRequest();
            xhr.open('POST', 'http://127.0.0.1:8000/server');
            xhr.send();
            xhr.onreadystatechange = function() {
                if (xhr.readyState === 4) {
                    if (xhr.status >= 200 && xhr.status < 300) {
                        result.innerHTML = xhr.response;
                    }
                }
            }
        });
    </script>
</body>

server.js 设置对应的请求段


app.post('/server', (request, response) => {
    response.setHeader(
        // 设置响应头,设置允许跨域
        'Access-Control-Allow-Origin', '*'
    );
    // 设置响应体
    response.send("hello ajax post");
});

4. POST请求中设置参数

写在send中,设置任意格式都可以,比较灵活,服务端需要有与之对应的处理方式

 xhr.send('a=100&b=200&c=300');
//或者
 xhr.send('a:100&b:200&c:300');

5. 设置请求头信息

// 设置请求头
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
// 设置请求体类型:参数查询字符串

添加自定义请求头时浏览器会有安全机制提示,在server.js中设置如下:

// all可以接收任意类型的请求:get/post/option/delete等
app.all('/server', (request, response) => {
    response.setHeader(
        // 设置响应头,设置允许跨域
        'Access-Control-Allow-Origin', '*'
    );
    response.setHeader('Access-Control-Allow-Headers', "*");
    // 设置响应体
    response.send("hello ajax post");
});

6. 服务端响应JSON数据

<script>
        const result = document.getElementById("result");
        // 键盘按下事件
        window.onkeydown = function() {
            const xhr = new XMLHttpRequest();
            xhr.open('GET', 'http://127.0.0.1:8000/json-server');
            xhr.send();
            xhr.onreadystatechange = function() {
                if (xhr.readyState === 4) {
                    if (xhr.status >= 200 && xhr.status < 300) {
                        console.log(xhr.response);
                        result.innerHTML = xhr.response;
                    }
                }
            }

        }
    </script>

send()只能接受字符串/buffer,设置Object类型需要先转化


app.all('/json-server', (request, response) => {
    const data = {
        name: 'atguigu'
    };
    // 对对象进行字符串转化
    let str = JSON.stringify(data);
    // 设置响应体
    response.send(str);
});
// 1. 自动对数据转化
 xhr.responseType = 'json';
 console.log(xhr.response);
 result.innerHTML = xhr.response.name;
// 2. 手动对数据转化
 let data = JSON.parse(xhr.response);
 console.log(data);
 result.innerHTML = data.name;

7. IE缓存问题

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值