var http = require('http');
var data = {key: 'value', hello: 'world'};
var server = http.createServer(function (req, res) {
res.writeHead(200,{"Content-Type":"text/plain","Access-Control-Allow-Origin":"http://localhost"}); //记得把"Access-Control-Allow-Origin":"http://localhost"这句写上,用于跨域访问
res.end(JSON.stringify(data));
}).listen(8080)
html代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
</body>
<script src="jquery.js"></script>
<script>
$.ajax({
type: "POST",
url: "http://localhost:8080",
data: null,
success: function(data) {
console.log(data,'data')
},
error:function(x,y,z){
console.log(x,y,z)
}
});
</script>
</html>
最后代码在服务器上运行就可以了