AJAX cross domain call

前端 未结 11 1083
温柔的废话
温柔的废话 2020-11-22 03:43

I know about AJAX cross-domain policy. So I can\'t just call \"http://www.google.com\" over a ajax HTTP request and display the results somewhere on my site.

I tried

11条回答
  •  春和景丽
    2020-11-22 04:10

    The only (easy) way to get cross-domain data using AJAX is to use a server side language as the proxy as Andy E noted. Here's a small sample how to implement that using jQuery:

    The jQuery part:

    $.ajax({
        url: 'proxy.php',
        type: 'POST',
        data: {
            address: 'http://www.google.com'
        },
        success: function(response) {
            // response now contains full HTML of google.com
        }
    });
    

    And the PHP (proxy.php):

    echo file_get_contents($_POST['address']);
    

    Simple as that. Just be aware of what you can or cannot do with the scraped data.

提交回复
热议问题