javascript 如何在不重新加载或重新呈现页面的情况下将 JSON 从 node.js 后端返回到前端?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11215724/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I return JSON from node.js backend to frontend without reloading or re-rendering the page?
提问by gruuuvy
I am working with node.js.
我正在使用 node.js。
I want to press a search button, make some rest api calls to another server in the backend and return the json back to the front end, and reload a div in the front end so that I won't have to refresh the page. Now I know I can reload just a div with jQuery or just Javascript dom manipulation.
我想按下一个搜索按钮,对后端的另一台服务器进行一些rest api调用并将json返回到前端,然后在前端重新加载一个div,这样我就不必刷新页面了。现在我知道我可以只用 jQuery 或 Javascript dom 操作重新加载一个 div。
But how do I make it call a method on the server side? I could have a submit button and it will make a post request and I can catch it and make my api calls from there, however from the node.js side, when I return, I will have to render the page again. How do I go about returning JSON from the back end to the front end without re-rendering or refreshing my page?
但是如何让它在服务器端调用一个方法呢?我可以有一个提交按钮,它会发出一个发布请求,我可以捕获它并从那里进行我的 api 调用,但是从 node.js 端,当我返回时,我将不得不再次呈现页面。如何在不重新渲染或刷新页面的情况下将 JSON 从后端返回到前端?
Thanks.
谢谢。
回答by Justin Thomas
Roughly something like this on the server:
服务器上大致是这样的:
var app = express.createServer();
app.use(express.bodyParser());
app.post('/search', function(req, res){
search_form = req.body; // <-- search items
MySearch.doSearch(search_form,function(err,items) {
res.send(items);
});
});
app.listen(3000);
You will have to implement the doSearch code to return whatever you are searching....
您将必须实现 doSearch 代码以返回您正在搜索的任何内容....
Client:
客户:
<script>
$.ajax( {
url: '/search',
data: search_form,
type: 'POST',
success: function(items) {
/* do something with items here */
// You will likely want a template so you don't have to format the string by hand
for( var item in items ) {
$('#results').append('<div>'+item.interestingField+'</div>);
}
}
});
</script>
回答by Brandon Boone
The following demonstrates how to make a basic AJAX request.
下面演示了如何进行基本的 AJAX 请求。
Fiddle: http://jsfiddle.net/tWdhy/1/
小提琴:http: //jsfiddle.net/tWdhy/1/
$(function(){
$.ajax({
url: '/echo/json/', //the URL to your node.js server that has data
dataType: 'json',
cache: false
}).done(function(data){
//"data" will be JSON. Do what you want with it.
alert(data);
});
});
? ?
? ?
回答by freakish
Summarizing comments (sadly, the question is not asked properly): what you want is to make an AJAX call to the server which is on a differentdomain, then the JavaScript. Normally you can't do that because of the same origin policy. There are however some hacks:
总结评论(遗憾的是,问题没有正确提出):您想要的是对位于不同域上的服务器进行 AJAX 调用,然后是 JavaScript。通常,由于同源策略,您不能这样做。然而,有一些黑客:
1) Use jQuery's AJAX with dataType: 'jsonp'
. You should read and learn more about JSONP.
1) 将 jQuery 的 AJAX 与dataType: 'jsonp'
. 您应该阅读并了解有关 JSONP 的更多信息。
2) Make an AJAX call to your domain and let the server call another server. With Node.JS you can use this:
2) 对您的域进行 AJAX 调用,让服务器调用另一台服务器。使用 Node.JS 你可以使用这个:
var http = require('http');
http.request(/* options */);
See the documentation.
请参阅文档。