Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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