If your simple test page is on a different protocol / domain / port than in your node.js hello world, you are making cross-domain requests and violating the same origin policy so your jQuery ajax (get and load) calls fail. To get this working cross-domain, you must use the JSONP format. For example node.js code:
var http = require('http'); http.createServer(function (req, res) { console.log('request received'); res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('_testcb(\'{"message": "Hello world!"}\')'); }).listen(8124);
and the client side of JavaScript / jQuery:
$(document).ready(function() { $.ajax({ url: 'http://192.168.1.103:8124/', dataType: "jsonp", jsonpCallback: "_testcb", cache: false, timeout: 5000, success: function(data) { $("#test").append(data); }, error: function(jqXHR, textStatus, errorThrown) { alert('error ' + textStatus + " " + errorThrown); } }); });
There are other ways to do this, for example, by setting up a reverse proxy server or completely creating your web application using a framework such as express .
yojimbo87 Mar 21 2018-11-11T00: 00Z
source share