在浏览器中使用fetch加载js并调用方法
本文于 375 天之前发表,文中内容可能已经过时。
1 2 3 4 5 6 7 8 9 10
| fetch('https://cdn.bootcdn.net/ajax/libs/dayjs/1.11.9/dayjs.min.js') .then(response => response.text()) .then(text => { eval(text); const now = dayjs(); console.log(now.format('YYYY-MM-DD HH:mm:ss')); }).catch(err => { console.log(err); });
|
在JavaScript中,fetch API返回的Response对象还提供了其他一些属性和方法,可以根据需要来获取数据或者执行其他操作。以下是一些常用的Response对象的属性和方法:
1、json()
这个方法返回一个Promise,解析为JSON格式的响应体
1 2 3 4
| fetch('https://api.example.com/data') .then(response => response.json()) .then(data => console.log(data)) .catch(error => console.error('Error fetching data:', error))
|
2、blob()
这个方法返回一个Promise,将响应体解析为二进制数据(Blob对象)
1 2 3 4 5 6
| fetch('https://example.com/image.png') .then(response => response.blob()) .then(blob => { }) .catch(error => console.error('Error fetching image:', error));
|
3、arrayBuffer()
这个方法返回一个Promise,将响应体解析为ArrayBuffer
1 2 3 4 5 6
| fetch('https://example.com/file') .then(response => response.arrayBuffer()) .then(arrayBuffer => { }) .catch(error => console.error('Error fetching file:', error));
|
4、text()
它返回一个Promise,将响应体解析为文本
1 2 3 4
| fetch('https://example.com/text') .then(response => response.text()) .then(text => console.log(text)) .catch(error => console.error('Error fetching text:', error));
|
eval函数
eval函数是JavaScript中的一个内置函数,用于将传入的字符串作为代码来执行。当使用eval函数时,传入的字符串会被当作JavaScript代码来执行,这可以用来动态执行代码或者动态生成代码。然而,由于eval函数的执行会影响性能和安全性,通常不推荐在实际项目中使用eval函数,除非确实有必要使用它。因为eval函数的执行会增加代码的复杂性,并且可能存在安全风险,因此应该尽量避免使用它。