导航栏: 首页 评论列表

XHR fetch dataURI

默认分类 2021/10/21 02:44

dataURI + XHR

let xhr = new XMLHttpRequest()
xhr.open('GET', 'data:text/json,{"a":123}', true)
xhr.onreadystatechange = () => {
  if (xhr.readyState !== 4) return ''
  console.log('xhr.readyState: ' + xhr.readyState)
  console.log('xhr.status: ' + xhr.status)
  console.log('xhr.responseText: ' + xhr.responseText)
}
xhr.send()

dataURI + base64 + XHR

let xhr = new XMLHttpRequest()
xhr.open('GET', 'data:text/json;base64,eyJhIjoxMjN9', true)
xhr.onreadystatechange = () => {
  if (xhr.readyState !== 4) return ''
  console.log('xhr.readyState: ' + xhr.readyState)
  console.log('xhr.status: ' + xhr.status)
  console.log('xhr.responseText: ' + xhr.responseText)
}
xhr.send()

dataURI + fetch

fetch('data:text/json,{"a":123}')
.then(function(res) {
  // res.text() 和 res.json() 均会返回一个Promise对象
  return res.json()
})
.then(function(data) {
  console.log(data)
})


>> 留言评论