javascript - Cannot pass JSON object because xmlhttp.status === 0 -
before mark duplicate, please understand have gone through solutions possible before posting question. code follows:
server.js
var http = require('http'); var foo = function (response) { response.writehead(200, {"content-type": "application/json"}); var otherarray = ["item1", "item2"]; var otherobject = { item1: "item1val", item2: "item2val" }; var json = json.stringify({ anobject: otherobject, anarray: otherarray, another: "item" }); response.end(json); // console.log(json + ' sent'); }; http.createserver(function (request, response) { foo(response); }).listen(8080);
client.js
var server = function() { var obj = {}; xmlhttp = new xmlhttprequest(); xmlhttp.onreadystatechange = function() { if (xmlhttp.readystate === 4) { if(xmlhttp.status === 200) { obj = json.parse(xmlhttp.responsetext); } else { alert('readystate === 4 status === ' + xmlhttp.status); } } }; xmlhttp.open("get", "http://localhost:8080", true); xmlhttp.send(); }; server();
i trying pass json object server client. run server using node server.js , open index.html sources client.js. using console.log, have found server.js
functioning fine , client.js
indeed affecting index.html
. however, when try above snippet of code, empty object in obj
. answers on stackoverflow have suggested setting security.fileuri.strict_origin_policy
value in firefox false have done, yet still getting status === 0
.
it sounds having cross domain issue since index.html
file not being served server.js
, treated local file. try having server.js
show index.html
when go root path localhost:8080
.
refer using node.js simple web server serving simple static web file. may want http://expressjs.com/ handles lot of basic stuff web server should do.
as firefox setting security.fileuri.strict_origin_policy, looks me allow reference static local files local file not make ajax requests.
Comments
Post a Comment