使用js从其他页面获取数据?

时间:2015-09-27 22:36:23

标签: javascript jquery html json ajax

我想从js或jquery或ajax但不是php获取其他页面的数据。我刚刚在堆栈溢出时找到了一个示例。 (here is the link)但是当我在index.html文件中编写这些代码时,它并不起作用。我不明白理由。这是我的代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
    <script src="//code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
    <script type=”text/javascript”>
       var url='http://query.yahooapis.com/v1/public/yql?q=select * from html where url=\'https://stackoverflow.com/\' and xpath=\'//div[@id="question-mini-list"]//h3//a\'&format=json&callback=?';


        $.getJSON( url, function(data){
            $.each(data.query.results.a, function(){       
                $('body').append('<div><a href="http://stackoverflow.com'+this.href +'">'+this.content+'</a></div>');    
             });
        });
    </script>
</head>
<body>  
    <div></div>
</body>
</html>

2 个答案:

答案 0 :(得分:1)

它的工作原理......

&#13;
&#13;
var url='http://query.yahooapis.com/v1/public/yql?q=select * from html where url=\'http://stackoverflow.com/\' and xpath=\'//div[@id="question-mini-list"]//h3//a\'&format=json&callback=?';


$.getJSON( url, function(data){
    $.each(data.query.results.a, function(){       
        $('body').append('<div><a href="http://stackoverflow.com'+this.href +'">'+this.content+'</a></div>');    
    });
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div></div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

我解决了我的问题这些代码,谢谢你的帮助

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
    <script src="//code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
    <script type="text/javascript" language="javascript">
        $(document).ready(function(){
            var url = 'deneme.html';
            $.get(url, function(response) {
                $('div#external').html($(response).find('#content>ul>li').html());
            });
        });
      </script>
    <style> body, html{padding: 0; margin:0;}</style>
</head>
<body>  
    <div id="external" >

    </div>
</body>
</html>