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
网友评论