两天试图通过简单的ajax请求访问aliexpress.com主页而没有运气,这并不像我所期望的那样容易。
有关访问策略和源问题的所有错误。
有人能给我jquery代码这样做吗?
我的代码
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();
错误:
发布于 2018-02-27 09:39:50
从错误消息来看,单靠jQuery是无能为力的。您必须考虑到同源政策 for 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>
https://stackoverflow.com/questions/49005087
复制相似问题