如何从服务器端javascript从不同的域获取.txt文件并获取它的客户端
我正在尝试从没有CORS或webservices的其他域上的Web服务器获取.txt文件。 我想我需要在服务器端用Node.js和JQuery来做,但我不知道从哪里开始。 我目前正在挑战Node和JQuery。
该文件位于http://epec.saw.usace.army.mil/dsskerr.txt
为此,您需要编写一个Node app服务器端。
var express = require("express"), app = express(), request = require("request"); var port = process.env.VCAP_APP_PORT || 8080; app.use(express.static(__dirname + '/public')); app.listen(port); app.get("/data", function (req, res) { request.get("http://epec.saw.usace.army.mil/dsskerr.txt").pipe(res); });
在客户端,您可以执行以下操作。
Simple app to fetch a txt file simple app to fetch a txt file through a node server to get around CORS
这是一个完整的git repo解决方案。 https://github.com/jsloyer/node-fetch-txt-file 。
此外,您可以通过单击下面的按钮将解决方案部署到Bluemix。