7 Replies - 829 Views - Last Post: 04 July 2015 - 01:49 PM

#1 Vally7   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 29-January 10

Node.js GET request

Posted 01 July 2015 - 06:28 PM

Hello. I'm trying to contact a node.js heroku app through a local web page. The node.js app takes a parameter from the url, something like "domain.com?/data=some" and it's supposed to respond with the same data. When I try to access it from a browser it works, displaying the same data, but when I access it through the web page I get an empty text as a response. What could be the problem?

Node.js app :
var http = require('http');
var url = require('url');
http.createServer(function (request, response) {
var _get = url.parse(request.url, true).query; 
response.write(String(_get.data));
response.end();
}).listen(process.env.PORT || 5000);



Web page relevant code:

function Modify(id)
{
     var req;
     req = new XMLHttpRequest();
     req.open("GET", "http://example.com/?data=" + id, true);
     req.send();
     alert(req.responseText);
}


All I get is "Javascript Alert" with no text. Thank you!

This post has been edited by Dormilich: 02 July 2015 - 03:09 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Node.js GET request

#2 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4202
  • View blog
  • Posts: 13,275
  • Joined: 08-June 10

Re: Node.js GET request

Posted 02 July 2015 - 12:59 AM

that’s because AJAX (as the name suggests) is asynchronous. when calling the send() method the request is sent off and then immediately JS proceeds with the next statement without waiting for the AJAX response (because for that you use an event handler).

This post has been edited by Dormilich: 02 July 2015 - 01:22 AM

Was This Post Helpful? 0
  • +
  • -

#3 Vally7   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 29-January 10

Re: Node.js GET request

Posted 02 July 2015 - 01:46 AM

Thank you! I tried to add an event handler, but it doesn't seem to work either.

function Modify(id)
{
     var req;
     req = new XMLHttpRequest();
     req.onreadystatechange = function()
     {
       if (req.readyState == 4 && req.status == 200)
         alert(req.responseText);
     }
     req.open("GET", "http://localhost:5000/?data=" + id, true);
     req.send();
}

This post has been edited by Vally7: 02 July 2015 - 01:47 AM

Was This Post Helpful? 0
  • +
  • -

#4 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4202
  • View blog
  • Posts: 13,275
  • Joined: 08-June 10

Re: Node.js GET request

Posted 02 July 2015 - 02:36 AM

open your browser's dev tools and check in the network tab which data are sent/retrieved by AJAX.

but I guess you’ve run into SOP since you use a different port than for the browser.
Was This Post Helpful? 0
  • +
  • -

#5 Vally7   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 29-January 10

Re: Node.js GET request

Posted 02 July 2015 - 03:00 AM

I checked it with the dev tools. The request is successful(status 200), but there is no response text. How can I solve SOP? Is it possible to have a webpage access a server on a different host(heroku in this case)?
Was This Post Helpful? 0
  • +
  • -

#6 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4202
  • View blog
  • Posts: 13,275
  • Joined: 08-June 10

Re: Node.js GET request

Posted 02 July 2015 - 03:11 AM

to resolve SOP you must enable CORS on both the client and the server.

you best ask the Heroku staff how to do that for your app.
Was This Post Helpful? 0
  • +
  • -

#7 Vally7   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 29-January 10

Re: Node.js GET request

Posted 02 July 2015 - 03:27 PM

I managed to get the html page on the heroku server and now I'm trying the following thing, hopefully to get rid of SOP. If I get a 'GET' request with no data I display the html page, if the request has data then I'm sending that data back. I tested it locally and I get the page, but I can't load the resources(some images in the same folder) to display it properly. I had managed to have it displayed properly before with the express.static method, but if I'm using that I don't know how to do the request with data. Here is the previous code I used
var express = require('express');
var app = express();

app.use(express.static(__dirname + '/public'));

app.listen(process.env.PORT || 5000);


Here is my current code:
var http = require('http');
var url = require('url');
var fs = require('fs');
fs.readFile(__dirname + '/public/index.html', function (err, html) {
    if (err) {
        throw err; 
    } 
http.createServer(function (request, response) {
var _get = url.parse(request.url, true).query; 
if(_get.data)
{
	response.write(String(_get.data));
	response.end();
}
else
{
	response.writeHeader(200, {"Content-Type": "text/html"});  
        response.write(html);  
        response.end(); 
}
}).listen(process.env.PORT || 5000);
});


Is it possible to display the page properly, with all its js interactivity and also receive and respond to requests with data?

This post has been edited by Vally7: 02 July 2015 - 03:30 PM

Was This Post Helpful? 0
  • +
  • -

#8 Vally7   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 29-January 10

Re: Node.js GET request

Posted 04 July 2015 - 01:49 PM

I managed to solve the problem with an express object that served the static content after "example.com/game" and managed the data separately after "example.com?data=".
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1