2010-08-11 136 views
0

我試圖使用jQuery獲取www.google.com/movies的響應文本。谷歌網站上的jQuery

我從$.load試圖$.get$.ajax,等...

任何人都可以告訴我,如果這是可能的嗎?

我的一些失敗的嘗試:

$(document).ajaxError(function() { 
    alert('ajax error'); 
}); 

$(document).load("http://www.google.com/movies?near=joinville,Santa+Catarina,Brazil", null, function (responseText) { 
    alert("Response:\n" + responseText); 
}); 

$.getJSON("http://www.google.com/movies?near=joinville,Santa+Catarina,Brazil&callback=?", 
    function (responseText) { 
    if (responseText) 
     alert("Response:\n" + responseText); 
    else 
     alert("fail"); 
    } 
); 

$.get("http://www.google.com/movies?near=joinville,Santa+Catarina,Brazil", 
    null, 
    function (responseText) { 
    if (responseText) 
     alert("Response:\n" + responseText); 
    else 
     alert("you fail again"); 
    } 
); 

$.ajax({ 
    cache: false, 
    url: "http://www.google.com/movies?near=joinville,Santa+Catarina,Brazil", 
    dataType: 'html', 
    success: function (responseText) { 
    if (responseText) 
     alert("Response:\n" + responseText); 
    else 
     alert("you fail again"); 
    } 
}); 
+3

使用谷歌的搜索API http://code.google.com/apis/ajaxsearch/ – Adam 2010-08-11 00:22:44

回答

3

老一套禁止跨域請求:
http://en.wikipedia.org/wiki/Same_origin_policy

最常見的解決辦法是做服務器端此查詢。 (我認爲谷歌沒有爲其電影服務提供AJAX API)

+0

我很害怕這個......但是一些奇怪的事情正在發生這裏。嘗試使用Chrome來運行這個jQuery。 $ .getJSON(「http://www.google.com/movies?near=joinville,Santa+Catarina,Brazil&callback=?」, function(responseText){ if(responseText) alert(「Response:\ n「+ responseText); else alert(」fail「); } ); 打開鉻開發工具,你會看到一個錯誤(右下角的小紅色圖標)。打開這個錯誤,你會看到我想要的響應文本(google html),但顯然它不是一個json響應,所以當它試圖解析結果時會引發錯誤。怎麼樣? – 2010-08-11 13:05:33

+0

@Marcelo谷歌怎麼會知道你期望頁面被返回爲JSON或JSONP而不是HTML? – 2010-08-11 16:34:29