简体   繁体   English

如何在从 POST 请求接收到变量后将变量传递给 GET 请求

[英]How to pass variable to GET request after receiving it from POST request

Can't figure out how to pass a variable to GET request after receiving it from the frontend.从前端接收到变量后,无法弄清楚如何将变量传递给 GET 请求。

 app.post('/subcategories', (req, res) => {
  //receive category id from front
   const category_id = req.body.category;
 })

 app.get('/subcategories', (req, res) => {
    // need to replace the 25 with the variable I recieved from the above POST request
    const category_id = 25;
    db.query('SELECT subcategory_name FROM subcategories WHERE category_id=(?)',
    [category_id],
    (err, result) =>{
      res.send({result})
    });
 })

You can directly pass the category_id to GET using query parameters.您可以使用查询参数直接将 category_id 传递给 GET。

Do something like:做类似的事情:

/subcategories?category_id=25

catch it in the GET api using:使用以下命令在 GET api 中捕获它:

let category_id = req.query.category_id

you can pass category_id as a query when making a get request and grab the category_id in the '/subcategories' route using req.query .您可以在发出获取请求时将category_id作为查询传递,并使用req.query'/subcategories'路由中获取category_id

make get requests like this.像这样获取请求。

/subcategories?categoryid=25

get it in a query object like this.像这样在查询 object 中获取它。

let categoryId = req.query.categoryid

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

相关问题 发送ajax POST请求,接收GET请求 - Sending ajax POST request receiving GET request 如何在 Post/Get 请求后在 Node/Express 中定义全局变量? - How to define Global Variable in Node/Express after Post/Get Request? 如何将GET响应数据传递到POST请求 - How to pass GET response data to POST Request 收到来自 fetch POST 请求的响应后,如何将用户重定向到页面? - How to redirect user to a page after receiving the response from a fetch POST request? 如何将变量从get请求传递到部分把手页面 - how to pass a variable from a get request into a partial handlebars page Django收到POST请求后未重定向 - Django is not redirecting after receiving a POST request 如何在空手道中动态传递发布请求参数以获取请求 - How to pass post request parameters dynamically to get request in Karate 如何将数据从POST请求传递到请求到外部服务? - How to pass data from the POST request to the request to external service? 如何将 POST 请求中的数据保存为变量,以在 GET 请求中使用? - How do I save my data from my POST request, as a variable, to use in my GET request? 如何在 JavaScript 中的 POST 请求 URL 中传递变量? - How do I pass a variable inside a POST request URL in JavaScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM