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

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

javascript - XMLHttpRequest Origin is not allowed by Access-Control-Allow-Origin

I'm trying to fetch some simple text over HTTP from JavaScript:

$(function() {
    $.get("http://mydomain.com/path", function(result) {
        console.log(result);
    });
});

The result should be text/html, but is basically only a simple key-value string, no HTML tags whatsoever. The page is in my control, but is supplied by a third-party (closed-source) so I can't modify the Java that serves out this page.

How can I get the contents of this page from JavaScript?

See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

You've simply gotta use either PHP/ASP your serverside language to retrieve the cross-domain request or use a service such as a cross domain ajax service created by Yahoo!.

It uses JSONP which is allowed to do very strict cross-domain requests, but asking from Yahoo! they will retrieve any page on the internet.

http://james.padolsey.com/javascript/cross-domain-requests-with-jquery/


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...