简体   繁体   English

如何将数据从前端jQuery传递到后端node.js

[英]How to pass data from frontend jQuery to backend node.js

I sent the <%=searchString%> to the frontend at the first place. 我首先将<%= searchString%>发送到前端。 Then, I trying to add a sort function on my search page. 然后,我尝试在搜索页面上添加排序功能。 How can I get the searchString value send back to my backend when redo the search with a sorting function. 使用排序功能重做搜索时,如何获取searchString值发送回我的后端。

node.js segment code node.js段代码

 router.get('/search', function(req, res){ console.log("Searching " + JSON.stringify(req.query)); var results = []; for(var i in restaurants) { for(var j in restaurants[i]["categories"]) { var category = restaurants[i]["categories"][j]; if(req.query.q && req.query.q.indexOf(category) != -1) { results.push({ name: restaurants[i]["name"], price: restaurants[i]["priceCat"], categories: restaurants[i]["categories"] }); } } } console.log("Search Results: " + JSON.stringify(results)); res.render('pages/search', { pageTitle: "search", searchString: req.query.q, results : results }); }); 
 <%- contentFor('body') %> <h1>Search Results: <small><%=searchString%></small></h1> <% if (results.length === 0) { %> <h4>No results found</h4> <% } else { %> <h2><small>Sort by:</small> <div class="row-sm-1"> <form action="/search" method="GET"> <div class="form-group"> <a href="/search"> <button type="submit" name="price" class="btn btn-primary btn-lg">Price</button> </a> </div> </form> </div> </h2> <table class="table"> <tr> <th>Name</th><th>Price</th><th>Category</th> </tr> <% for(var i = 0; i < results.length; i++) {%> <tr> <td><%= results[i].name%></td> <td><%= results[i].price%></td> <td> <% for(var j = 0; j < results[i].categories.length; j++) {%> <%= results[i].categories[j]%><% if(j != results[i].categories.length - 1) {%>,<%}%> <%}%> </td> </tr> <%}%> <%}%> </table> 

You can use $.post in jquery to hit the route defined in node.js/express.js in your case. 您可以在jquery中使用$ .post来达到在您的情况下在node.js / express.js中定义的路由。

I think you're missing the basic mind mapping about the architectural style. 我认为您缺少有关建筑风格的基本思维导图。 REST architecture can be used for this task. REST体系结构可用于此任务。

Explanation on REST : What exactly is RESTful programming? REST解释: RESTful编程到底是什么?

This would help to get the basic idea as well 这也将有助于获得基本思想

在此处输入图片说明

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

相关问题 如何在 javascript 前端使用 async await 从 async await node.js 后端接收数据 - how to use async await on javascript frontend to receive data from async await node.js backend 如何从后端获取角度数据并通过node.js发送给前端? - How get data with angular from the backend and send it with node.js for the frontend? 如何使用来自后端的数据和 node.js 在前端绘制图表? - How to plot a chart in the frontend using data from the backend with node.js? 如何从后端节点 js 获取 Json 数据到前端 html - How to get Json data from backend node js to frontend html 来自前端的数据在后端重复两次 React.js Node.js - Data coming from frontend duplicates in backend twice React.js Node.js 如何在 node.js 后端使用 usestate 并响应前端 - how to use usestate with node.js backend and react frontend 使用纯 js 将数据从 node.js 发送到前端 - Send data from node.js to frontend using pure js 如何从前端react.js中的node.js从多个数据库中获取数据 - How to fetch data from multiple db from the node.js in frontend react.js 如何从 js 前端的节点后端访问值 - How to access a value from node backend in js frontend 如何在不重新加载或重新呈现页面的情况下将node.js后端的JSON返回到前端? - How can I return JSON from node.js backend to frontend without reloading or re-rendering the page?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM