我使用crossdomain ajax來獲取完整的html頁面代碼,但它總是對失敗的按摩進行響應。如何使用jsonp提醒整個html代碼?
consolelog: 「未捕獲的SyntaxError:意外的標記<」
這是我的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」方法來解決這個問題。
任何人有任何的答案PLZ告訴我,THX一個地段
!!更新!
我使用代理來解決這個問題。
首先你必須創建一個代理文件,我用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(){
\t $.get("proxy.php?uri=", function(data) {
\t alert(data);
\t x(data);
\t }); \t
}
function x(data){
\t $.get("proxy.php?uri="+data.toString().split('"')[5], function(data2) {
\t alert("Data Loaded: " + data2);
\t });
}
</script>
<body onload="t()">
</body>
</html>
它似乎工作!但我得到一個新的問題。這似乎來自谷歌服務器的錯誤。 我不能使用ajax訪問谷歌imagesearch,它會迴應403錯誤或302移動。
PS.Maybe我需要一個谷歌的API或我輸給添加任何代碼?
THX大家:)
改變你的成功功能,並讓我知道會發生什麼:成功:功能(數據){警報(數據); }, – Jay
jsonpurl +'?callback = anycallbackfunction' –
@Jay它沒有工作:'( – MIKE