Fetch

作者: 草帽lufei | 来源:发表于2019-08-26 20:46 被阅读0次

get url file fetch fetch url json

Sample Demo

fetch('http://example.com/movies.json')
  .then(function(response) {
    return response.json();
  })
  .then(function(myJson) {
    console.log(JSON.stringify(myJson));
  });

Uploading json data

var url = 'https://example.com/profile';
var data = {username: 'example'};

fetch(url, {
  method: 'POST', // or 'PUT'
  body: JSON.stringify(data), // data can be `string` or {object}!
  headers:{
    'Content-Type': 'application/json'
  }
}).then(res => res.json())
.then(response => console.log('Success:', JSON.stringify(response)))
.catch(error => console.error('Error:', error));

upload file

// upload file
let formData = new FormData()
let file = new File([this.fileObj], fileName.toString(), {
  'type': 'audio/wave'
})
console.log('file: ', file)

formData.append('image', file)
fetch('http://127.0.0.1:3000/file_upload', {
  method: 'POST',
  body: formData
}).then((res) => {
  console.log('res: ', res)
}).catch((err) => {
  console.error('err: ', err)
})

https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch

相关文章

网友评论

      本文标题:Fetch

      本文链接:https://www.haomeiwen.com/subject/ladwoqtx.html