我正在 Pycharm 中开发 Django 项目。我需要在 Javascript 文件中获取 API,但它给了我一个内部服务器错误,并在控制台中显示了获取行的错误。看起来它根本不识别 fetch。
这是我如何在代码中使用 fetch 的示例;
document.querySelector('#compose-form').onsubmit = function () {
fetch('/emails', {
method: 'POST',
body: JSON.stringify({
recipients: in_recipients,
subject: in_subject,
body: in_body,
})
})
.then(response => response.json())
.then(result => {
if (result.message !== "Email sent successfully."){
alert(result.error)
}
});
};
我的代码实际上可以在 Vscode 上运行,但无法在 Pycharm 中运行。我试过了
pip install fetch
我收到以下错误;
ERROR: Command errored out with exit status 1:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "C:\Users\bilge\AppData\Local\Temp\pip-install-n1v3op9a\fetch\setup.py", line 6, in <module>
description = file(os.path.join(here, 'README.txt')).read()
NameError: name 'file' is not defined
----------------------------------------
ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full
command output.
也尝试过
python setup.py egg_info
说:
can't open file 'setup.py': [Errno 2] No such file or directory
提前谢谢您。
fetch 需要进行多填充,因为当您编译代码时,您不会添加任何可能需要的新全局或原始属性。 fetch 将是全局命名空间上的一个新属性,因此需要对其进行填充。
所以它们不能用babel编译。在此处查看更多详细信息使用 babel-preset-env 时 Babel 不进行 Polyfilling Fetch
没有这样的事
pip install fetch
您需要更改设置、语言和框架,并找到 Javascript 库。添加 TypeScript。