我正在尝试使用ajax调用我的node js服务器,并将json正文返回给客户端。
以下是我的ajax调用代码。
self.information = function() {
$.ajax({
type: 'GET',
url: 'http://localhost:3000/getIOT',
contentType: 'application/json; charset=utf-8'
})
.done(function(result) {
console.log(result);
})
.fail(function(xhr, status, error) {
console.log(error);
})
.always(function(data){
});
}
}
以下是 Node.js 代码示例。
app.get('/getIOT', function (req, res , err) {
request({
'auth': {
'user': 'masnad',
'pass': 'whatstodaysrate',
'sendImmediately': true
},
url: 'https://get.com/getAuroraRate?Amount=1000',
method: 'GET',
}, function (error, request, body) {
console.log(body);
return response.end(JSON.stringify(body));
})
});
我遇到的错误是net::ERR_CONNECTION_REFUSED,而且响应体没有返回到ajax调用中。
/Users/nihit/Documents/node/multiple-js/server/server.js:36
return response.end(JSON.stringify(body));
^
ReferenceError: response is not defined
at Request._callback (/Users/nihit/Documents/node/multiple-js/server/server.js:36:20)
at Request.self.callback (/Users/nihit/Documents/node/multiple-js/node_modules/request/request.js:186:22)
at emitTwo (events.js:106:13)
at Request.emit (events.js:192:7)
at Request.<anonymous> (/Users/nihit/Documents/node/multiple-js/node_modules/request/request.js:1081:10)
at emitOne (events.js:96:13)
at Request.emit (events.js:189:7)
at IncomingMessage.<anonymous> (/Users/nihit/Documents/node/multiple-js/node_modules/request/request.js:1001:12)
at Object.onceWrapper (events.js:291:19)
at emitNone (events.js:91:20)
我不确定我做错了什么。
Origin 'servername' is not allowed by Access-Control-Allow-Origin.
? - Colm Seale