Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

jquery - What prevents me from using $.ajax to load another domain's html?

My domain:

<!DOCTYPE html>  
<html>
<head>
<title>scrape</title>
<script src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
</head>
<body>
    <script>
        $.ajax({url:'http://their-domain.com/index.html',
        dataType:'html',
            success:function(data){console.log(data);}
        });
    </script>
</body>
</html>

What prevents me from being able to scrape their-domain? Any work around?

Addendum: thank you all for the suggestions to use a server side script, but I am for the moment interested in solving this problem exclusively using the client.

If I format the request using "jsonp" I do at least get a response, but with the following error:"Uncaught SyntaxError: Unexpected token <". So I am getting a response from their-domain but the parser expects it to be json. (As well it should.) I am hacking through this trying to see if their is a way to trick the client into accepting this response. Please understand that I know this is atypical.

<!DOCTYPE html>  
<html>
<head>
<title>scrape</title>
<script src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
</head>
<body>
    <script>
        $.ajax({url:'http://their-domain.com/index.html',
        dataType:'jsonp',
            success:function(data){console.log(data);}
        });
    </script>
</body>
</html>
See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

There are Four ways to get around Same Origin Policy

  1. Proxy - You request it from your server, your server requests it from other domain, your server returns it to the browser
  2. Flash cross domain policy - other domain must add a crossdomain.xml file to their site
  3. Cross domain HTTP header - other domain must add an Access-Control-Allow-Origin header to their page
  4. JSONP - It's a json web service that provides a callback function. Other domain must implement this.

Note: The ONLY way to do it without the other domain's help is #1, routing it through your own server.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...