博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Javascript] Fetch API
阅读量:6904 次
发布时间:2019-06-27

本文共 790 字,大约阅读时间需要 2 分钟。

fetch() does the same thing as XHR, but fetch return a promise.

 

fetch('password.txt', {  'method': 'PUT',  'headers': {    'X-Something-nothing': 'fetch rocks!'  }}).then( response => {  if(response.status === 200){    return response.text()  }else{    throw "Cannot fetch data"  }}).then( data => {  console.log(data);}).catch( err => {  console.error(err)})

 

Check the reponse API here:

Besides text(), you can use json() or blob().

 

'no-cors' and opaque responses

If I request //google.com from this site using XHR or plain fetch it will fail. This is because it's a CORS request and the response doesn't have CORS headers.

However, with fetch, you can make a no-cors request:

fetch('//google.com', {  mode: 'no-cors'}).then(function(response) {  console.log(response.type); // "opaque"});

 

转载地址:http://bmldl.baihongyu.com/

你可能感兴趣的文章
Spring 3.1 Environment Profiles--转载
查看>>
Python 的三目运算
查看>>
继承、复写、重载等总结
查看>>
SQL 语句转换格式函数Cast、Convert
查看>>
其实,SSL也不是配通了就什么都不管的~~
查看>>
请不要用SECONDS_BEHIND_MASTER来衡量MYSQL主备的延迟时间【转】
查看>>
10个强大的Javascript表单验证插件推荐
查看>>
北邮iptv用WindowsMediaplayer打不开的解决的方法
查看>>
软件发布版本区别介绍-Alpha,Beta,RC,Release
查看>>
cdoj 1143 传输数据 最大流
查看>>
loadrunner 学习笔记--AJAX
查看>>
a5调试
查看>>
cocoa 的大招(KVC的几点强大应用记录)
查看>>
IOS7 导航栏适配二
查看>>
第1章 游戏之乐——NIM(3)两堆石头的游戏
查看>>
eclipse中新建python项目报错:Project interpreter not specified
查看>>
如何在Linux上实现文件系统的自动检查和修复?
查看>>
jquery ajax调用返回json格式数据处理
查看>>
奥姆卡剃刀原理
查看>>
数据结构(C实现)------- 单链表
查看>>