• <bdo id='mRGbF'></bdo><ul id='mRGbF'></ul>
  • <small id='mRGbF'></small><noframes id='mRGbF'>

    <i id='mRGbF'><tr id='mRGbF'><dt id='mRGbF'><q id='mRGbF'><span id='mRGbF'><b id='mRGbF'><form id='mRGbF'><ins id='mRGbF'></ins><ul id='mRGbF'></ul><sub id='mRGbF'></sub></form><legend id='mRGbF'></legend><bdo id='mRGbF'><pre id='mRGbF'><center id='mRGbF'></center></pre></bdo></b><th id='mRGbF'></th></span></q></dt></tr></i><div id='mRGbF'><tfoot id='mRGbF'></tfoot><dl id='mRGbF'><fieldset id='mRGbF'></fieldset></dl></div>

  • <legend id='mRGbF'><style id='mRGbF'><dir id='mRGbF'><q id='mRGbF'></q></dir></style></legend>

    <tfoot id='mRGbF'></tfoot>

        post 方法的问题(使用 fetch 和 express)

        时间:2023-10-02

        <small id='svPLp'></small><noframes id='svPLp'>

        <tfoot id='svPLp'></tfoot>
        <i id='svPLp'><tr id='svPLp'><dt id='svPLp'><q id='svPLp'><span id='svPLp'><b id='svPLp'><form id='svPLp'><ins id='svPLp'></ins><ul id='svPLp'></ul><sub id='svPLp'></sub></form><legend id='svPLp'></legend><bdo id='svPLp'><pre id='svPLp'><center id='svPLp'></center></pre></bdo></b><th id='svPLp'></th></span></q></dt></tr></i><div id='svPLp'><tfoot id='svPLp'></tfoot><dl id='svPLp'><fieldset id='svPLp'></fieldset></dl></div>

              <tbody id='svPLp'></tbody>

                  <bdo id='svPLp'></bdo><ul id='svPLp'></ul>
                  <legend id='svPLp'><style id='svPLp'><dir id='svPLp'><q id='svPLp'></q></dir></style></legend>
                • 本文介绍了post 方法的问题(使用 fetch 和 express)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我是一个非常初学者,所以我希望我的问题不是那么愚蠢.我想要做的是将经度和纬度从客户端 javascript 传递到服务器端的 node.js 中.我正在使用 fetch 和 express.js.

                  I'm a very beginner so I hope my question is not that stupid. What I want to do is to pass a longitude and a latitude from a client-side javascript into a node.js on a server-side. I'm using a fetch and express.js.

                  在我的html代码下面:

                  Below my html code:

                  <!DOCTYPE html>
                  <html lang="en">
                    <head>
                      <meta name="viewport" content="width=device-width, initial-scale=1.0" />
                      <meta http-equiv="X-UA-Compatible" content="ie=edge" />
                    </head>
                    <body>
                        latitude: <span id="latitude"></span>&deg;<br />
                        longitude: <span id="longitude"></span>&deg;<br />
                      </p>
                      <button id="geolocate">geolocate</button>
                      <script src="main.js"></script>
                    </body>
                  </html>
                  

                  这是我的 main.js 中的一个小示例:

                  Here's a little sample from my main.js:

                  document.getElementById('geolocate').addEventListener('click', event => { 
                  
                      if ('geolocation' in navigator) { 
                          console.log('geolocation available');
                          navigator.geolocation.getCurrentPosition(position => {                    
                              var X = position.coords.latitude;   
                              var Y = position.coords.longitude;
                              console.log(X, Y);
                              document.getElementById('latitude').textContent = X;
                              document.getElementById('longitude').textContent = Y;
                              
                               const data = {X, Y}
                               const options = {
                                  method: 'POST',
                                  body: JSON.stringify(data),
                                  headers: {
                                      'content-type': 'application/json'
                                   }
                               }
                          fetch('/api', options);
                        });
                  }  else {  
                          console.log('geolocation not available');
                        }
                  }); 
                  

                  在这里你可以看到我的 node.js 代码:

                  And here you can see my node.js code:

                  const express = require('express');
                  const app = express();
                  app.listen(3000, () => console.log('listening at 3000'));
                  
                  app.post('/api', (req, res) => {
                    console.log(req);
                  });
                  

                  当我运行它时,我收到一个 404 错误.我不知道我在这里做错了什么.如有任何建议,我将不胜感激.

                  When I run it I receive an 404 error. I have no idea what I'm doing wrong here. I'll be grateful for any advice.

                  这个应用程序正在我的 VPS 上运行.我还有一个带有 SSL 别名的域.为了运行 node.js,我使用 nodemon 作为进程.以下是日志:

                  This app is working on my VPS. I also have a domain with SSL aliases. To run node.js I use nodemon as a process. Here are the logs:

                  user_name_with_sudo  11451  0.5  3.6 663672 38152 pts/0    Sl+  11:05   0:00 node /bin/nodemon index.js $
                  

                  如您所见,这是一个过程.

                  As you can see it's a process.

                  httpd service status - Oct 20 17:14:21 www.{my domain}.pl systemd[1]: Started The Apache HTTP Server. 
                  

                  如你所见,我正在开发 centOS7

                  As you can see I'm working on centOS7

                  推荐答案

                  尝试为获取添加完整路径.您正在 localhost 的 3000 端口上侦听服务器

                  Try to add full path for the fetch. You are listening server at localhost on the port 3000

                  main.js:

                  document.getElementById('geolocate').addEventListener('click', event => { 
                  
                  if ('geolocation' in navigator) { 
                      console.log('geolocation available');
                      navigator.geolocation.getCurrentPosition(position => {                    
                          var X = position.coords.latitude;   
                          var Y = position.coords.longitude;
                          console.log(X, Y);
                          document.getElementById('latitude').textContent = X;
                          document.getElementById('longitude').textContent = Y;
                          
                           const data = {X, Y}
                           const options = {
                              method: 'POST',
                              body: JSON.stringify(data),
                              headers: {
                                  'content-type': 'application/json'
                               }
                           }
                      fetch('http://localhost:3000/api', options)
                          .then(response => response.json())
                          .then(data => {
                              // if everting is ok should log the object  message: "Long lang sent to express" from server
                              console.log(data)
                          });
                    });
                  ...
                  

                  服务器端就像 dat

                  const express = require('express');
                  const app = express();
                  app.listen(3000, () => console.log('listening at 3000'));
                  
                  app.post('/api', (req, res) => {
                    try {
                        const {X, Y} = req.body
                        console.log(X, Y)
                        res.status(200).json({ message: "Long lang sent to express" })
                     } catch (e) {
                         res.status(500).json({ message: 'Something go wrong, try again' })
                     }
                  });
                  

                  尝试使用小写变量(例如:不使用 X, Y.. 但使用 x, y)如果它不是常量 :) Gl 与编程

                  Trying to use lowercase variables(example: not use X, Y.. but use x, y) if it not a constants :) Gl with programming

                  这篇关于post 方法的问题(使用 fetch 和 express)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:为什么浏览器不运行&lt;脚本&gt;在通过 fetch API 检索的 HTML 片段中? 下一篇:IE11 中的中止控制器不起作用.任何方式来支持它

                  相关文章

                  <legend id='UbZtZ'><style id='UbZtZ'><dir id='UbZtZ'><q id='UbZtZ'></q></dir></style></legend>

                  <tfoot id='UbZtZ'></tfoot>

                  1. <small id='UbZtZ'></small><noframes id='UbZtZ'>

                      <bdo id='UbZtZ'></bdo><ul id='UbZtZ'></ul>

                    1. <i id='UbZtZ'><tr id='UbZtZ'><dt id='UbZtZ'><q id='UbZtZ'><span id='UbZtZ'><b id='UbZtZ'><form id='UbZtZ'><ins id='UbZtZ'></ins><ul id='UbZtZ'></ul><sub id='UbZtZ'></sub></form><legend id='UbZtZ'></legend><bdo id='UbZtZ'><pre id='UbZtZ'><center id='UbZtZ'></center></pre></bdo></b><th id='UbZtZ'></th></span></q></dt></tr></i><div id='UbZtZ'><tfoot id='UbZtZ'></tfoot><dl id='UbZtZ'><fieldset id='UbZtZ'></fieldset></dl></div>