Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Patrick Karkafi
Patrick Karkafi
508 Points

How to pass Javascript variables from Node.js (site.com:3000) to another domain (site.com)?

Hello, As you know, node.js does not have php integration, meaning that i can not send variables to my website with $_SESSION.

I need a way to pass a Javascript variable to my domain.

My node.js is on port 3000 (site.com:3000), and my website is site.com.

Example:

Node.js (site.com:3000): Server.js or app.js:

        var money = 5;

main.js (site.com):

   var currentCash = money;

So the variable money would be transferred from my node.js javascript files to my website's.

In case you're wondering, my website is a normal website with html5 and php, but it has a game on it that is run in node.

The variables i am sending are sensitive and should not be able to be changed when being sent over. (I need a secure way to pass them)

Thanks!

1 Answer

Hey Patrick - Why not just send a POST request to the server with the data that is needed:

If using jQuery ajax:

$.ajax({
    method: 'POST'
    url: // your url
    data: { current_cash: currentCash } // On the server this will be in $_POST['current_cash']
    success: function (response) {
        console.log(response);
    }
})