[英]React JS - Fetch() Event Results in 404 Not Found
I've created a React JS app and added a fetch()
event to a class but I'm unable to work out how to locate the file path as the resulting code leads to a 404 Not Found
error in the console. 我已经创建了一个React JS应用程序并向类中添加了一个fetch()
事件,但我无法找到如何找到文件路径,因为生成的代码导致控制台中出现404 Not Found
错误。
LoginForm.js:11 POST http://localhost:3000/api/user-login 404 (Not Found)
I am trying to locate user-login.js
within /src/api/user-login.js
我想在/src/api/user-login.js
找到user-login.js
This is my folder structure: 这是我的文件夹结构:
https://monosnap.com/direct/DT5zykUaHOVz8YMJy9O96B762bOsvQ https://monosnap.com/direct/DT5zykUaHOVz8YMJy9O96B762bOsvQ
Here is the relevant code within the class from LoginForm.js
: 以下是LoginForm.js
类中的相关代码:
class LoginForm extends React.Component {
handleSubmit(event) {
var user = {
'clientname': 'client name here',
'email': 'client email here'
};
fetch('/api/user-login', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(user)
})
}
render() {
// Stuff that gets rendered
}
}
I have attempted every variation of /app/user-login
that I can think of, including: 我已经尝试了我能想到的/app/user-login
每个变体,包括:
app/user-login
app/user-login.js
/app/user-login
/app/user-login.js
./app/user-login
./app/user-login.js
../app/user-login
../app/user-login.js
Could somebody please enlighten me on how I can link to this file? 有人可以告诉我如何链接到这个文件? Sorry if this is a stupid question, I've only been learning React JS for a couple of days. 对不起,如果这是一个愚蠢的问题,我只学了几天React JS。 Thanks for your help. 谢谢你的帮助。
I hadn't set-up an API server which is why this was showing a 404 error. 我没有设置API服务器,这就是显示404错误的原因。 I had to use ExpressJS which means that any time my React app makes a request to something that's not a static asset (not an image or CSS or index.html, basically), it will forward the request to the server. 我不得不使用ExpressJS,这意味着只要我的React应用程序向非静态资产(基本上不是图像或CSS或index.html)发出请求,它就会将请求转发给服务器。
I used the following tutorial to do this: 我使用以下教程来做到这一点:
https://daveceddia.com/create-react-app-express-backend/ https://daveceddia.com/create-react-app-express-backend/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.