【发布时间】:2015-12-18 09:35:42
【问题描述】:
我正在使用 XMLHttpRequest 从 javascript 发送 json 格式的数据,并在 node.js 文件中接收数据,如下所示
client.js
$.ajax({
type: "POST",
dataType: 'json',
data: JSON.stringify(Idata),
url: AjaxURL,
success: function (result) {
return true;
}
});
server.js
var http = require('http');
console.log("server initialized");
var server = http.createServer(function (req, response) {
req.on('data', function (data) {
var d = JSON.parse(data);
console.log("data : " + d.OperationType);
});
req.on('end', function () {
response.end();
});
}).listen(3000);
现在我成功获取了 operationtype 的值,但是在客户端浏览器中显示此错误:
XMLHttpRequest 无法加载 http://localhost:3000/。请求的资源上不存在“Access-Control-Allow-Origin”标头。 Origin 'http://localhost' 因此不允许访问。
【问题讨论】:
-
我从客户端发送的 Idata 如下: var Idata = { 'OperationType': 'GID', 'DNS': '' + objName + '', 'fn': 'SetGUID ' };
标签: javascript ajax node.js xmlhttprequest