【发布时间】:2016-11-17 05:07:11
【问题描述】:
我使用跨域 ajax 获取整个 html 页面代码,但它总是响应失败的按摩。
consolelog: " Uncaught SyntaxError: Unexpected token
这是我的 ajax 函数。
function t(){
var url = document.getElementById("url").value;
$.ajax({
url: 'https://www.google.com/',
type: 'GET',
crossDomain: true,
data:'',
dataType: 'jsonp',
success: function() { alert("Success"); },
error: function() { alert('Failed!'); },
});
}
PS。我不想使用“Access-Control-Allow-Origin”的方法来解决这个问题。
任何人有任何答案请告诉我,谢谢很多
!!更新!!
我使用代理解决了这个问题。
首先你必须创建一个代理文件,我以php为例。
proxy.php
<?php
$url ='https://www.google.com/searchbyimage?site=search&sa=X&image_url=http://kingofwallpapers.com/apple/apple-015.jpg';
if($_GET['uri']=='')
echo file_get_contents($url);
else
echo file_get_contents($_GET['uri']);
?>
test.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title></title>
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.12.4.min.js"></script>
</head>
<script type="text/javascript">
//var NewArray = data.toString().split("=");
function t(){
$.get("proxy.php?uri=", function(data) {
alert(data);
x(data);
});
}
function x(data){
$.get("proxy.php?uri="+data.toString().split('"')[5], function(data2) {
alert("Data Loaded: " + data2);
});
}
</script>
<body onload="t()">
</body>
</html>
这似乎工作!但是我遇到了一个新问题。这似乎是来自谷歌服务器的错误。 我无法使用 ajax 访问 google imagesearch,它会响应 403error 或 302 move。
PS.也许我需要一个 google API 或我丢失的任何代码来添加?
谢谢大家:)
【问题讨论】:
-
把你的成功函数改成这个,让我知道会发生什么:成功:函数(数据){警报(数据); },
-
jsonpurl + '?callback=anycallbackfunction'
-
@Jay 没用 :'(
-
@B.Kocaman 我不能使用它,因为当我使用 may 函数时它已经发送了一个回调 url
-
喜欢“google.com/…”
标签: javascript php html ajax jsp