node.js / jQuery跨域:ERR_CONNECTION_REFUSED

我是Node.js的新手

我创build了一个简单的节点/快速应用程序,它提供了一个包含一个button的单个网页,当点击这个button时,这个button向一个Express路线发出一个jQuery ajax请求。

路由callback会对一些包含外汇汇率的json数据产生一个http.get请求来打开。 JSON然后输出到开发工具控制台窗口。

该应用程序工作在第一次button点击,但随后的任何点击控制台窗口显示:

GET http://127.0.0.1:3000/getFx net::ERR_CONNECTION_REFUSED 

“开发工具”控制台窗口的屏幕抓图显示第一次单击的结果,然后显示连接被拒绝时的第二次单击。

在这里输入图像描述

错误细节如下:

 GET http://127.0.0.1:3000/getFx net::ERR_CONNECTION_REFUSED jquery-2.1.3.min.js:4 n.ajaxTransport.k.cors.a.crossDomain.send jquery-2.1.3.min.js:4 n.extend.ajax (index):18 (anonymous function) jquery-2.1.3.min.js:3 n.event.dispatch jquery-2.1.3.min.js:3 n.event.add.r.handle 

我简单的Node / Express应用程序如下所示:

 var express = require('express'); var app = express(); var http = require("http"); var data = ""; var json; console.log( "__dirname", __dirname ); app.use( express.static( __dirname + '/') ); var options = { host:"openexchangerates.org", path:"/api/latest.json?app_id=<get free ID from openexchangerates.org>" }; app.get("/", function( req, res ) { res.sendFile('index.html', { root: __dirname }); }) app.get("/getfx", function(req, res) { console.log("Route: getFx"); getFx(res); }) function getFx(res) { console.log("http getFx"); http.get(options, function (response) { response.on("data", function (chunk) { //console.log("data:\n"+chunk); data += chunk; }); response.on("end", function () { json = JSON.parse(data); console.log("http response end:"); res.end( data ); }); response.on("error", function (e) { console.log("error:\n" + e.message); }); }) } app.listen(3000); 

我的html索引页面是:

 <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title>Get FX</title> <script src="http://code.jquery.com/jquery-2.1.3.min.js"></script> <script> $(document).ready(function() { console.log( "document ready"); $("#btnFx").click(function() { console.log('clicked', this ); $.ajax({ url : "http://127.0.0.1:3000/getFx", dataType : "json", success : function(json) { console.log("json returned:\n", json); } }); } ); }) </script> </head> <body> <button id="btnFx" style="width:200px">Get foreign exchange rates</button> </body> 

对于openexchangerates.org来提供数据,一个免费的应用程序ID是必需的。 任何能够帮助解决这个问题的人都可能需要经历很短的注册时间:

这个链接在这里:

https://openexchangerates.org/signup/free

但是,对于那些具有更好的Node / Express / jQuery知识的人来说,我的错误可能会变得很明显。

提前谢谢了

您定义datajson vars的方式会导致后续请求失败。 既然你在前面定义了它们,所有的请求都会重新使用它们,这意味着当你为第二个请求使用JSON.parse datadata将包含两个有效的jsonstring,从而产生一个无效的jsonstring。 为了解决这个问题,在callback中定义datajson

 var express = require('express'); var app = express(); var http = require("http"); //var data = ""; //var json; console.log( "__dirname", __dirname ); app.use( express.static( __dirname + '/') ); var options = { host:"openexchangerates.org", path:"/api/latest.json?app_id=<get free ID from openexchangerates.org>" }; app.get("/", function( req, res ) { res.sendFile('index.html', { root: __dirname }); }) app.get("/getfx", function(req, res) { console.log("Route: getFx"); getFx(res); }) function getFx(res) { console.log("http getFx"); http.get(options, function (response) { var data = ""; var json; response.on("data", function (chunk) { //console.log("data:\n"+chunk); data += chunk; }); response.on("end", function () { console.log("http response end:"); json = JSON.parse(data); res.json(json); }); response.on("error", function (e) { console.log("error:\n" + e.message); }); }) } app.listen(3000); 

问题来自使用chrome在本地主机上发生的跨源请求保护。 尝试使用其他浏览器或只允许所有主机(*)或您的主机( http:// localhost:3000 )的起源:

 app.use( express.static( __dirname + '/') ); app.use(function(req,res,next){ res.header('Access-Control-Allow-Origin', '*'); res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS'); res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With'); // intercept OPTIONS method if ('OPTIONS' == req.method) { res.send(200); } else { next(); } });