js fetch

fetch能够执行XMLHttpRequest所有任务,更容易使用,接口更现代化

fetch必须是异步的

获取响应

 let r = fetch('weatherforecast')
 r.then((response) => { console.log(response)});

            r.then(response => {
                //返回状态码200
                console.log(response.status);
                //状态码在200-299之间时返回true
                console.log(response.ok); 
            });


response.text
获取数据

            let r = fetch('weatherforecast')
            r.then((response) => {
                response.text().then(data => {
                    console.log(data)
                });
            });

简写

            let r = fetch('weatherforecast')
            r.then(response => response.text())
                .then(data => console.log(data));

自定义请求

原文地址:https://www.cnblogs.com/buchizaodian/p/14015070.html