简体   繁体   English

Fetch Api 无法将内容发布到服务器

[英]Fetch Api is unable to post things to the server

I had created a chatroom in my website and I had used the default action of a form to post message's data into the server and store it in MongoDB.我在我的网站上创建了一个聊天室,并使用表单的默认操作将消息数据发布到服务器并将其存储在 MongoDB 中。 But, know I am trying to make it live by using fetch instead as the data will go the server without refreshing the page simultaneously.但是,知道我正在尝试通过使用 fetch 来实现它,因为数据将发送到服务器而不会同时刷新页面。 But, after posting the data to the server as JSON, the server is unable to get the data of the post.但是,在将数据以 JSON 格式发布到服务器后,服务器无法获取该帖子的数据。 I am also using the Body Parser to get the data in the req object, but, that too isn't working and it shows that the data is null.我也在使用 Body Parser 来获取 req 对象中的数据,但是,这也不起作用,它显示数据为空。 Can someone tell me how I can fix this issue?有人可以告诉我如何解决这个问题吗?

My HTML:我的 HTML:

 <form id="form-container"> <input type="text" id="user-name-input" name="userName"> <input required type="text" id="message-input" name="message" placeholder="Send a message here!"> <button type="submit" id="submit-button">Submit</button> </form>

My Client-Side Javascript:我的客户端 Javascript:

 submitButton.addEventListener('click', (e) => { e.preventDefault(); var message = document.getElementById('message-input').value; fetch('/dps/chat/request-message-insert', { method: 'POST', body: JSON.stringify({ userName: userName, message: message }), headers: { 'Content-Type': "application/json; charset=UTF-8" } }).then(res => res.json()).then(data => console.log(data)); })

My server.js file:我的 server.js 文件:

 app.post('/dps/chat/request-message-insert', urlencodedParser, (req, res) => { console.log(req.body) const userName = req.body.userName; const message = req.body.message; client.connect(async (err) => { const collection = client.db("gradilo").collection("chat-messages"); await collection.insertOne({ text: message, userName: userName }) await client.close(); }) })

I will check your code your server-side are perfect but I think the problem is client-side are a few issues.我会检查你的代码你的服务器端是否完美,但我认为问题是客户端有一些问题。

check this link same as your solution检查此链接与您的解决方案相同

I fixed my problem with a simple line of code.我用一行简单的代码解决了我的问题。 Looks like the client-side code and the server code is all fine.看起来客户端代码和服务器代码都很好。 We have also specified headers in the options of the fetch too.我们还在 fetch 的选项中指定了标题。 The problem was that the server didn't know in which format it could expect data from the client.问题是服务器不知道它可以从客户端获取数据的格式。 Thus, adding the following code before the app.post() method will fix this issue.因此,在 app.post() 方法之前添加以下代码将解决此问题。

 app.use(express.json());

Then, you do not even need the npm body-parser dependency to parse the incoming data.然后,您甚至不需要 npm body-parser 依赖项来解析传入的数据。 The data will be added to the req.body object without any additional middleware or dependency.数据将被添加到 req.body 对象中,无需任何额外的中间件或依赖项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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