one of the cross-domain solutions: jsonp
// server.js
var qs = require("queryString");
var http = require("http");
var server = http.createServer();
server.on("request", function(req, res) {
console.log("irene"); // irene
var params = qs.parse(req.url.split("?")[1]);
var fn = params.callback;
// jsonp
res.writeHead(200, { "Content-Type": "text/javascript" });
res.write(fn + "(" + JSON.stringify(params) + ")");
res.end();
})
server.listen("8094");
console.log("Server is running at port 8094...");
// jsonp.html
<html>
<head></head>
<body>
<script>
var script = document.createElement("script");
script.type = "text/javascript";
// onBack
script.src = "http://www.domain2.com:8094/login?user=admin&callback=onBack";
document.head.appendChild(script);
//
function onBack(res) {
alert(JSON.stringify(res));
}
</script>
</body>
</html>
Why does the "irene"" in server.js print twice when I visit jsonp.html, in my browser?