Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 424
gruuuvy Avatar asked Dec 20 '22 19:12

gruuuvy


1 Answers

The following demonstrates how to make a basic AJAX request.

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

​ ​

like image 82
Brandon Boone Avatar answered Dec 24 '22 02:12

Brandon Boone