Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I would like to retrieve the page data from a webserver in which the URL is

http://www.xxxx.com/data.txt

Is there a way with client side javascript to do this? Does the webserver have to be configured to allow it?

John

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
130 views
Welcome To Ask or Share your Answers For Others

1 Answer

You can't really do this in client side Javascript because of CORS. If the server you are requesting the data from supports CORS you can do it client side. However if the server you are requesting the data from does not support CORS you need to do the request server side and send it client side.

I would do this server side in a Node app and then fetch the data from your HTML page from the Node app. Here is a little node script to do it.

var express = require("express"),
    app = express(),
    request = require("request");

var port = process.env.VCAP_APP_PORT || 8080;

app.listen(port);

app.get("/data", function (req, res) {
    request.get("http://www.xxxx.com/data.txt").pipe(res);
});

And then with JQuery on the client side you could do the following.

    $.get("/data", function(data) {
        console.log(data);
    });

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...