Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/457.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将数据从POST移动到GET时出现问题-节点/Express_Javascript_Node.js_Express_Post_Get - Fatal编程技术网

Javascript 将数据从POST移动到GET时出现问题-节点/Express

Javascript 将数据从POST移动到GET时出现问题-节点/Express,javascript,node.js,express,post,get,Javascript,Node.js,Express,Post,Get,node/express是个新手,正在开发一个天气应用程序,该应用程序从一个表单接收一个位置,然后发布 <form method="POST" action="/"> <input id="input" type="text" name="city" placeholder="Search by name or zip code" /> <button id="button" type="submit"></button> </for

node/express是个新手,正在开发一个天气应用程序,该应用程序从一个表单接收一个位置,然后发布

<form method="POST" action="/">
  <input id="input" type="text" name="city" placeholder="Search by name or zip code" />
  <button id="button" type="submit"></button>
</form>

您需要将表单方法更改为
GET
(或者省略方法属性,
GET
是默认方法)。因此,当您提交表单时,请求将转到此路径

app.get("/", function(req, res) {
  const cityName = req.query.city; // see http://expressjs.com/en/5x/api.html#req.query
  const url = `https://api.openweathermap.org/data/2.5/weather?q=${cityName}&units=imperial&appid=${apiKey}`;

  ..
  code
  ..

  res.render("index", weather_data);
});

此路由
app.post(“/”,…)
可以删除。

如果我理解正确,您希望使用post请求向服务器发送某种数据,然后使用get请求检索它,对吗? 在这种情况下,首先需要一个数据库来存储数据。 所以,您将数据发送到服务器->将其存储在数据库->使用get请求从数据库检索数据

您可以同时使用SQL或NoSQL数据库。
我建议您将MongoDB与mongoose模块一起使用。

将逻辑从get映射提取到一个接受cityName作为参数的方法中。我不知道为什么express程序员忘记了它只是Javascript,你可以把普通代码分解成一个函数,然后从多个路径调用它。但是,这就是你在这里可以做的。
app.get("/", function(req, res) {
  const cityName = req.query.city; // see http://expressjs.com/en/5x/api.html#req.query
  const url = `https://api.openweathermap.org/data/2.5/weather?q=${cityName}&units=imperial&appid=${apiKey}`;

  ..
  code
  ..

  res.render("index", weather_data);
});