【问题标题】:POST request generates failed to fetchPOST 请求生成失败
【发布时间】:2021-11-28 17:35:15
【问题描述】:

我在通过 fetch API 从 javascript 发出 POST 请求时遇到问题,我收到以下错误:TypeError: Failed to fetch at fetchCallImpp at window.fetch 代码如下:

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)
});

希望您能帮我解决错误,非常感谢您提前

【问题讨论】:

  • 检查 DevTools 的 Network 选项卡以查看有关失败的更多详细信息。

标签: javascript post fetch fetch-api


【解决方案1】:

在其他浏览器中是否显示相同的错误? fetch 是 ES6 Javascript,可能你使用的浏览器不支持。

fetch(url, requestOptions)

另外,请确保定义了变量“url”。

【讨论】:

    猜你喜欢
    • 2020-08-28
    • 2011-09-07
    • 2013-02-08
    • 2013-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多