fetch-api

Equivalent of Fetch 'no-cors' mode in XHR or Ajax libraries?

限于喜欢 提交于 2021-02-18 21:00:44
问题 Is there a way to write fetch {mode: 'no-cors'} in an existing (and most browsers supported) HTTP request? As an example, I would like to know if it's possible to translate this: fetch(url, { mode: 'no-cors'}) .then(function (response) { console.log(response); }); Into a XMLHttpRequest or Ajax of some sort. I tried several ways and they behave differently - I just want the opaque response that fetch returns. Thanks! 来源: https://stackoverflow.com/questions/54660924/equivalent-of-fetch-no-cors

Equivalent of Fetch 'no-cors' mode in XHR or Ajax libraries?

强颜欢笑 提交于 2021-02-18 21:00:05
问题 Is there a way to write fetch {mode: 'no-cors'} in an existing (and most browsers supported) HTTP request? As an example, I would like to know if it's possible to translate this: fetch(url, { mode: 'no-cors'}) .then(function (response) { console.log(response); }); Into a XMLHttpRequest or Ajax of some sort. I tried several ways and they behave differently - I just want the opaque response that fetch returns. Thanks! 来源: https://stackoverflow.com/questions/54660924/equivalent-of-fetch-no-cors

How to use returned JSON error in a JavaScript async fetch call

馋奶兔 提交于 2021-02-17 05:49:08
问题 I have an async fetch call which calls my backend to create a customer with an email address. If successful, the JSON returned is sent to a doNextThing() function. If the backend returns a non-200 status code it also returns JSON like {"message": "Something went wrong"} . I want to catch the error and send that message to the console . I've read dozens of slightly similar questions and edged close to an answer. So far I have the below, but if the backend's response was a 403 status code, for

Conditional GET ignored with fetch API

喜你入骨 提交于 2021-02-11 15:06:32
问题 I am fetching and parsing an RSS feed (https://www.mangaupdates.com/rss.php) with Discord.js/Node.js. I'm currently trying to add a conditional If-Modified-Since header to make a conditional GET request to the RSS feed. Here is my code: fetch(mangaUpdatesRSS, { method: 'GET', headers: {'If-Modified-Since': new Date().toUTCString()} }) .then(res => { console.log(res.status); The response status is always 200 even though it should only be making a request if it's been modified since the time of

POST request Fetch API prevent redirect

好久不见. 提交于 2021-02-10 17:48:43
问题 So i want to make a pure html and javascript form and submit it to server. Here is my html form code: <form id="email-signup" action="http://www.server.com" method="post"> <input id="firstname-input" type="hidden" name="firstname" value=""> <input type="text" name="email" placeholder="Input Email"> <input type="hidden" name="campaign[id]" value="1"> <input type="hidden" name="campaign[name]" value="Text Campaign"> <input type="submit" value="Submit"> </form> And here is my javascript code:

How to Post data to database using Fetch API in React.js

懵懂的女人 提交于 2021-02-10 06:10:32
问题 I have recently learned how to set up a simple api in my express server using a localhost mySQL database I have running on a MAMP server. After I set that up I learned how to have React.js fetch that data and display it. Now I want to do the reverse and post data from a form I created in React.js. I would like to continue using the fetch API to post that data. You can view my code below. Below is my express server code for my api. app.get('/api/listitems', (req, res) => { connection.connect()

How to Post data to database using Fetch API in React.js

元气小坏坏 提交于 2021-02-10 06:09:21
问题 I have recently learned how to set up a simple api in my express server using a localhost mySQL database I have running on a MAMP server. After I set that up I learned how to have React.js fetch that data and display it. Now I want to do the reverse and post data from a form I created in React.js. I would like to continue using the fetch API to post that data. You can view my code below. Below is my express server code for my api. app.get('/api/listitems', (req, res) => { connection.connect()

Fetch blob from URL and write to file

纵饮孤独 提交于 2021-02-08 18:31:18
问题 I'm trying to fetch some binary data (MP3) from a server and then store it in a file: var fs = require ('fs'); var fetch = require ('node-fetch'); fetch ( audioUrl, { method: 'GET', headers: { 'Accept': '*/*' } } ) .then ((res) => res.blob()) .then ((blob) => { console.log ('Blob size: ', blob.size); fs.writeFile ( `filename.mp3`, blob, // BUG HERE (err) => { if (err) { console.log (`Error writing audio ${audioIdx}`, err); } } ); }) The problem is marked at BUG HERE . I'm passing a blob to fs

Fetch blob from URL and write to file

心已入冬 提交于 2021-02-08 18:29:57
问题 I'm trying to fetch some binary data (MP3) from a server and then store it in a file: var fs = require ('fs'); var fetch = require ('node-fetch'); fetch ( audioUrl, { method: 'GET', headers: { 'Accept': '*/*' } } ) .then ((res) => res.blob()) .then ((blob) => { console.log ('Blob size: ', blob.size); fs.writeFile ( `filename.mp3`, blob, // BUG HERE (err) => { if (err) { console.log (`Error writing audio ${audioIdx}`, err); } } ); }) The problem is marked at BUG HERE . I'm passing a blob to fs

Catching “Failed to load resource” when using the Fetch API

|▌冷眼眸甩不掉的悲伤 提交于 2021-02-08 14:44:27
问题 I'm trying to catch a bunch of errors related to the same origin policy when using the Fetch API but without any success: window.onerror = (message, file, line, col, error) => console.log(error) window.addEventListener('error', (error) => console.log(error)) try { fetch('https://www.bitstamp.net/api/ticker/').catch(e => { console.log('Caugth error:') console.log(e) console.log(JSON.stringify(e)) }) } catch (e) { console.log('try-catch') console.log(e) } The errors I want to catch only appear