How To Fetch A .txt File From A Different Domain From Server Side Javascript And Get It Client Side
I'm trying to fetch a .txt file from a webserver on a different domain which has no CORS or webservices. I think I need to do it on server side with Node.js and JQuery, but I'm not
Solution 1:
To do this you will need to write a little Node app server side.
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);
});
On the client side you could do the following.
<html><head><title>Simple app to fetch a txt file</title></head><body><h2>simple app to fetch a txt file through a node server to get around CORS</h2><divclass="data"></div><scriptsrc="https://code.jquery.com/jquery-2.1.3.min.js"></script><script>
$(function() {
$.get("/data", function(data) {
console.log(data);
$(".data").append(data);
});
});
</script></body></html>
Here is a full git repo with the solution. https://github.com/jsloyer/node-fetch-txt-file.
Additionally you can deploy the solution to Bluemix by clicking the button below.
Post a Comment for "How To Fetch A .txt File From A Different Domain From Server Side Javascript And Get It Client Side"