0

两天试图通过简单的 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();

错误: 在此处输入图像描述

4

1 回答 1

1

从错误信息来看,单靠 jQuery 是帮不上忙的。您必须考虑 JavaScript 的同源策略。您可能需要考虑在您的域中创建代理脚本。

代理看起来像这样:/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>
于 2018-02-27T09:39:50.847 回答