简体   繁体   中英

POST request generates failed to fetch

I'm having problems making a POST request through the fetch API from javascript and I get the following error: TypeError: Failed to fetch at fetchCallImpp at window.fetch The code is the following:

 const agregarCafetalDB = (nombre) => { const data = { "nombre": `${nombre}`, }; const requestOptions = { method: 'POST', body: JSON.stringify(data), redirect: 'follow', mode: 'cors', headers: { 'Content-Type': 'application/json', 'Access-Control-Allow-Origin': '*' }, }; fetch(url, requestOptions).then(response => response.json()).then(result => console.log(result)).catch(error => console.log('error', error)); } btnAgregar.addEventListener('click', (e) => { agregarCafetalDB(cafetal.value) });

I would like you to help me solve the error thank you very much in advance

Does the same error shows in other browsers? fetch is ES6 Javascript, maybe the browser you are using doesn't support it.

 fetch(url, requestOptions)

Also, make sure that variable 'url' is defined.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM