jQuery获取另一个网站页面内容/ div内容

时间:2016-02-23 11:05:31

标签: javascript jquery html ajax

我尝试从网站抓取一些跟踪信息并尝试使用loadajax,但我收到以下错误:

  

XMLHttpRequest无法加载http://www.tuffnells.co.uk/PODLookupResults.aspx?__EVENTTARGET=&__EVENTARGU ...%24tbDestPostcode = AL15BY& ctl00%24maincontent%24btnDoPODLookup =再次搜索+。 No' Access-Control-Allow-Origin'标头出现在请求的资源上。

我尝试过:

<div id="tracking"></div>
<script type="text/javascript">
  $(document).ready(function(){
    $("#tracking").load( "http://www.tuffnells.co.uk/PODLookupResults.aspx?__EVENTTARGET=&__EVENTARGUMENT=&__VIEWSTATEGUID=7ca82b1d-b722-4cdc-b74a-b338d8577ffa&__VIEWSTATE=&__EVENTVALIDATION=%2FwEdAAevVXD1oYELeveMr0vHCmYPaomE%2FDwQD43eOdzEj3p%2Fm4U4pgxq6tlupSJfQZQBazFFj%2F1LmlGLyHFagz1yHZm8bjowVgAJ8C3e%2B2bVMPt91KjXCHjnAsonQDi2zFSuasUVzpitHiLDCDtiLHCjNCQG4CxrbV5VPFqBeOgs2X52AD%2FEb%2BYR%2BEJ68PaN2CiyKzE%3D&ctl00%24ctl16%24tbHeaderSearch=Search..&ctl00%24maincontent%24tbAccountRef=01484267&ctl00%24maincontent%24tbConsignmentRef=2837&ctl00%24maincontent%24tbDestPostcode=AL15BY&ctl00%24maincontent%24btnDoPODLookup=Search+Again #ctl00_maincontent_pnlPODRecords" );
  });
</script>

<div id="tracking"></div>
  <script type="text/javascript">
    $(document).ready(function(){
      var url = "http://www.tuffnells.co.uk/PODLookupResults.aspx?__EVENTTARGET=&__EVENTARGUMENT=&__VIEWSTATEGUID=7ca82b1d-b722-4cdc-b74a-b338d8577ffa&__VIEWSTATE=&__EVENTVALIDATION=%2FwEdAAevVXD1oYELeveMr0vHCmYPaomE%2FDwQD43eOdzEj3p%2Fm4U4pgxq6tlupSJfQZQBazFFj%2F1LmlGLyHFagz1yHZm8bjowVgAJ8C3e%2B2bVMPt91KjXCHjnAsonQDi2zFSuasUVzpitHiLDCDtiLHCjNCQG4CxrbV5VPFqBeOgs2X52AD%2FEb%2BYR%2BEJ68PaN2CiyKzE%3D&ctl00%24ctl16%24tbHeaderSearch=Search..&ctl00%24maincontent%24tbAccountRef=01484267&ctl00%24maincontent%24tbConsignmentRef=2837&ctl00%24maincontent%24tbDestPostcode=AL15BY&ctl00%24maincontent%24btnDoPODLookup=Search+Again";
      $.ajax({
        url:url,
          type:'GET',
          success: function(data){
            $('#tracking').html($(data).find('#ctl00_maincontent_pnlPODRecords').html());
          }
      });
    });
  </script>

有人有什么想法吗?我需要将跟踪详细信息放入我们的跟踪页面,因为他们没有提供API。

3 个答案:

答案 0 :(得分:0)

答案盯着你!

 No 'Access-Control-Allow-Origin' header is present on the requested resource.

答案 1 :(得分:0)

使用Javascript无法实现,而是使用PHP:

    $.ajax({url: 'loadRemoteFile.php', type: 'POST', data: {loadPage: url}, success: function(data){
doStuff();
}});

和您的PHP文件:

if (isset($_POST['loadPage'])){
    echo file_get_contents($_POST['loadPage']);

答案 2 :(得分:0)

我发现link可能对您有所帮助。

由于