TypeError: Failed to execute #39;fetch#39; on #39;Window#39;: Invalid value(TypeError:无法在“窗口上执行“获取:无效值)
问题描述
我尝试使用 fetch 从后端调用 react,没有库(例如 Axios).所以我创建了这个函数:
}并这样称呼它
我在这个函数中调用了我的 api():
这是我的完整组件:
}
但是当我尝试运行它时,我收到了这样的错误
<块引用>TypeError: 无法在 'Window' 上执行 'fetch': 无效值
有人知道我的代码有什么问题吗?该功能在我使用POST"方法时有效,但在我使用GET"方法时无效
当我尝试将 Authorization
标头添加到我的 fetch 调用时,我也遇到了这种情况.在我的情况下,它是由标题字符串中的换行符引起的,即 Bearer:
somelong-token
.将新行更改为空格即可解决问题.
I've tried to use fetch to call from backend using react, without libs (such as Axios). So I created this function:
} and call it like this
i call my api() inside this function:
this is my full component:
}
but when i tried to run it, i got an error like this
TypeError: Failed to execute 'fetch' on 'Window': Invalid value
Is there anyone who knows what's wrong with my code? The function works when I use the "POST" method, but it doesn't work when I use "GET" method
This also happened to me when I tried to add an Authorization
header to my fetch calls. In my case it was caused by a newline character in the header string, i.e. Bearer:
somelong-token
. Changing the new line to a space solved the problem.
这篇关于TypeError:无法在“窗口"上执行“获取":无效值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!