node.js - Javascript xmlhttp get request to nodejs server returns nothing -
before begin i'd tell of made lot of searching solution of problem on own.
here nodejs server:
var http = require('http'); http.createserver(function (req, res) { console.log("recived request url " + req.url); var sname = req.url.search("name"); if(sname != -1){ sname = sname + "name".length + 1; var = sname; while(req.url[sname] != '?' && sname<req.url.length){ sname++; } var name = req.url.substring(from,sname); console.log("returning parameter of name - " + name); res.writehead(200, {'content-type': 'text/plain'}); res.end(name+'\n'); } else{ res.writehead(200, {'content-type': 'text/plain'}); res.end('error - ask name\n'); } }).listen(1337, '127.0.0.1'); console.log('server running @ http://127.0.0.1:1337/'); it listens on port 1337 , if request url correct returns string.
here javascript code asking nodejs answer.
function httpget(theurl){ var xmlhttp = null; xmlhttp = new xmlhttprequest(); xmlhttp.open( "get", theurl, true ); xmlhttp.send( null ); return xmlhttp.responsetext; } var url = httpget("http://127.0.0.1:1337/?name=mateusz"); settimeout(function(){ document.getelementbyid("actualnewsspace").innerhtml=url; var xurl = httpget("http://127.0.0.1:1337/?"+url); },5000) returned xmlhttp.responsetext blank. why? that's question.
that's nodejs server has in matter
recived request url /?name=mateusz returning parameter of name - mateusz recived request url /? if curl server returns mi proper value.
xmlhttp.responsetext hasn't been populated when return it. ajax asynchronous.
see using xmlhttprequest , bind event handler listen when http response has arrived , process data in that.
Comments
Post a Comment