最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can I return JSON from node.js backend to frontend without reloading or re-rendering the page? - Stack Overflow

programmeradmin2浏览0评论

I am working with 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.

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?

Thanks.

I am working with 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.

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?

Thanks.

Share Improve this question edited Jun 26, 2012 at 21:01 gruuuvy asked Jun 26, 2012 at 20:51 gruuuvygruuuvy 2,1294 gold badges33 silver badges56 bronze badges 6
  • You're talking about AJAX right? jQuery AJAX. – Brandon J. Boone Commented Jun 26, 2012 at 20:54
  • Are you simply asking how to do AJAX from browser to server? Or are you asking how to make HTTP call from the server to another server? Cause I'm a bit confused. – freakish Commented Jun 26, 2012 at 20:54
  • Or are you asking how to use node.js to only show json with no html? – cuzzea Commented Jun 26, 2012 at 20:56
  • I'm asking how to do AJAX from browser to server. I want the browser to call the server, which will make some REST calls, and return JSON back to browser. Without refresh. – gruuuvy Commented Jun 26, 2012 at 20:56
  • I tried to use jQuery AJAX, but I couldn't make rest calls to another server because of Same origin policy. – gruuuvy Commented Jun 26, 2012 at 20:59
 |  Show 1 more ment

3 Answers 3

Reset to default 5

The following demonstrates how to make a basic AJAX request.

Fiddle: http://jsfiddle/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./

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....

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 ments (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. 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'. You should read and learn more about JSONP.

2) Make an AJAX call to your domain and let the server call another server. With Node.JS you can use this:

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

See the documentation.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论