ajax 请求获取速卖通主页

ajax request to get aliexpress home page

两天尝试通过简单的 ajax 请求到达 aliexpress.com 主页,没有运气,这并不像我预期的那样容易。

有关访问策略和来源问题的所有错误。

任何人都可以给我 jquery ajax 代码来做到这一点。

我的代码

  function setHeader(xhr) {
     xhr.setRequestHeader('Access-Control-Allow-Origin', '*');
     xhr.setRequestHeader('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
    }
    //url: 'https://www.aliexpress.com',

    function getHomePage() {
        $.ajax({
            url: 'https://www.aliexpress.com',
            type: 'GET',
            callback: '?',
            data: '',
            datatype: 'text/html',
            contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
            crossDomain: true,
            success: function (data) { alert(data); },
            error: function () { alert('Failed!'); },
            beforeSend: setHeader
        });

    } //end getHomePage

通话:

getHomePage();

错误:

从错误信息来看,单靠jQuery是无济于事的。您必须考虑 JavaScript 的 Same origin policy。您可能需要考虑在您的域中创建代理脚本。

代理看起来像这样:/get_ali_express.php

<?php
    echo file_get_contents("https://www.aliexpress.com");
?>

还有 js:有些地方说,/index.html

<script>
    function getHomePage() {
        $.ajax({
            url: '/get_ali_express.php',
            type: 'GET',
            success: function (data) { alert(data); },
            error: function () { alert('Failed!'); }
        });
    } 
</script>