jqueryajaxaliexpress

ajax request to get aliexpress home page


two days trying to reach aliexpress.com homepage with simple ajax request with no luck, it's not easy as i expect .

all errors around access policy and origin issues.

can any body give me jquery ajax code to do that.

MY CODE

  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

Call:

getHomePage();

Errors: enter image description here


Solution

  • From the error message, jQuery alone can't help. You have to take the Same origin policy for JavaScript into account. You may want to consider to creating a proxy script at your domain.

    The proxy would look something like this: /get_ali_express.php

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

    And the js: some where in say, /index.html

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