【发布时间】:2014-03-17 08:15:03
【问题描述】:
Node.js - Javascript - HTML - XMLHttpRequest 无法加载
基本上我在一个 node.js 和 1 个 javascript/html 下发布了 2 个脚本
我将数据发布到我的 node.js 文件,但我收到此错误并且在我的 html 页面上看不到 node.js 的结果。
XMLHttpRequest cannot load http://192.168.2.109:8111/?name=nknk. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://192.168.2.109:8111' is therefore not allowed access
这是我的 Node.js 文件:
var url = require('url');
var http = require('http');
var server = http.createServer(function (request, response) {
var queryData = url.parse(request.url, true).query;
response.writeHead(200, {"Content-Type": "text/plain"});
if (queryData.name) {
var basevalue = queryData.name;
var value = basevalue.split (":");
var exec = require('child_process').exec;
console.log(value[0]);
console.log(value[1]);
exec ("casperjs test.js " + value[0] + " " + value[1] + '\n',function(err, stdout, stderr) {
response.end(stdout);
});
} else {
response.end("Contact Admin - Not Working\n");
}
});
server.listen(1234);
这是我的 html/javascript:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<html><head><title>Welcome To ....</title>
<script type="text/javascript">
function textareaToArray(t){
return t.value.split(/[\n\r]+/);
}
function showArray(msg){
for(i = 0; i < msg.length; i++) {
// something per item
var data = {}; //your own data
$.post("http://192.168.2.109:8111" + "?" + $.param({name: msg[i]}), data);
}
// the old code
document.getElementById("message").innerHTML = msg.join("
");
}
</script>
</head>
<body>
<h1> WELCOME TO .... </h1>
<form>
<textarea rows="10" cols="60" name="alpha"></textarea>
<br>
<input type="button" value="show array" onclick="showArray(textareaToArray(this.form.alpha ))">
</form>
<br>
<textarea id="message" rows="6" cols="60" name="message"></textarea>
</body></html>
有人可以帮我解决这个问题,以便我可以将结果返回到我的 html 中而不会出现此错误
----edit
这是我正在尝试的代码,我仍然看不到来自 node.js 服务器的响应数据 我需要查看我的节点服务器运行的 exec 命令的响应我知道这需要大约 40 秒才能完成,但我仍然没有看到任何输出到 html 的内容
node.js
var url = require('url')
var http = require('http')
var server = http.createServer(function (request, response) {
var queryData = url.parse(request.url, true).query;
response.writeHead(200, {"Content-Type": "text/plain"});
if (queryData.name) {
// user told us their name in the GET request, ex: http://host:8000/?name=Tom
var basevalue = queryData.name;
var value = basevalue.split (":");
console.log(value[0]);
console.log(value[1]);
var exec = require('child_process').exec;
exec ("casperjs test.js " + value[0] + " " + value[1] + '\n',function(err, stdout, stderr) {
response.end('_stdout(\'{"content": "blablabla"}\')');
});
} else {
response.end("Contact Admin - Not Working\n");
}
});
// Listen on port 8000, IP defaults to 127.0.0.1
server.listen(8999);
~
javascript html
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<html><head><title>Welcome To ....</title>
<script type="text/javascript">
function textareaToArray(t){
return t.value.split(/[\n\r]+/);
}
function showArray(msg){
for(i = 0; i < msg.length; i++) {
// something per item
// var data = {}; //your own data
//$.post("http://192.168.2.109:8121" + "?" + $.param({name: msg[i]}), data);
$.ajax({
url: 'http://192.168.2.109:8999' + '?' + $.param({name: msg[i]}),
dataType: "jsonp",
jsonpCallback: "_stdout",
cache: false,
timeout: 5000,
success: function(data) {
function doSomethingWithData(data) { $('#message').val(data.content); }
},
error: function(jqXHR, textStatus, errorThrown) {
handleError(data);
}
});
}
// the old code
document.getElementById("message").innerHTML = msg.join("
");
}
</script>
</head>
<body>
<h1> WELCOME TO .... </h1>
<form>
<textarea rows="10" cols="60" name="alpha"></textarea>
<br>
<input type="button" value="show array" onclick="showArray(textareaToArray(this.form.alpha ))">
</form>
<br>
<textarea id="message" rows="6" cols="60" name="message"></textarea>
</body></html>
【问题讨论】:
标签: javascript html node.js cors