简体   繁体   English

如何在不重新加载或重新呈现页面的情况下将node.js后端的JSON返回到前端?

[英]How can I return JSON from node.js backend to frontend without reloading or re-rendering the page?

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. 我想按下一个搜索按钮,对后端的另一台服务器进行一些休息api调用,然后将json返回到前端,并在前端重新加载一个div,这样我就不必刷新页面了。 Now I know I can reload just a div with jQuery or just Javascript dom manipulation. 现在我知道我可以用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. 我可以有一个提交按钮,它会发出一个帖子请求,我可以抓住它并从那里进行api调用,但是从node.js那边,当我返回时,我将不得不再次渲染页面。 How do I go about returning JSON from the back end to the front end without re-rendering or refreshing my page? 如何在不重新渲染或刷新页面的情况下将JSON从后端返回到前端?

Thanks. 谢谢。

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);
    }); 
});

​ ​

http://expressjs.com/ http://expressjs.com/

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>

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 different domain, then the JavaScript. 总结评论(遗憾的是,问题没有被正确地问到):你想要的是对不同域上的服务器进行AJAX调用,然后是JavaScript。 Normally you can't do that because of the same origin policy. 通常,由于相同的原始政策,您不能这样做。 There are however some hacks: 然而有一些黑客:

1) Use jQuery's AJAX with dataType: 'jsonp' . 1)使用jQuery的AJAX和dataType: 'jsonp' You should read and learn more about JSONP. 您应该阅读并了解有关JSONP的更多信息。

2) Make an AJAX call to your domain and let the server call another server. 2)对您的域进行AJAX调用,让服务器调用另一台服务器。 With Node.JS you can use this: 使用Node.JS,您可以使用:

var http = require('http');
http.request(/* options */);

See the documentation . 请参阅文档

暂无
暂无

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

相关问题 如何在不重新加载URL的情况下查询Node.Js的后端并更新页面? - How to query the backend of my Node.Js and updating the page without reloading the URL? 如何防止在React中使用钩子重新渲染页面? - How can I prevent re-rendering of the page with hooks in React? 如何在不重新渲染未删除的项目的情况下从列表中删除项目? - How can I delete an item from list without re-rendering undeleted Items? 如何将数据从前端jQuery传递到后端node.js - How to pass data from frontend jQuery to backend node.js 在Node.js/Express中等待后端数据库更新后如何更新前端? - How can I update the frontend after waiting for the database in the backend to update in Node.js/Express? JavaScript从rest API获取JSON而无需重新呈现页面 - JavaScript get JSON from rest API without re-rendering the page 如何从后端节点 js 获取 Json 数据到前端 html - How to get Json data from backend node js to frontend html 从NodeJS发送数据而不重新渲染整个页面? - Sending data from NodeJS without re-rendering the whole page? Backbonejs,在(调整大小)事件调用时更改View上的事件,而无需重新加载或重新呈现页面 - Backbonejs, changing events on View upon a (resize) event call without reloading or re-rendering page 如何在没有前端的情况下使用 Postman 在 Node.js Express 后端服务器上测试 Passport JS Google OAuth - How to test Passport JS Google OAuth on Node.js Express backend server without a frontend using Postman
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM