2011-04-25 104 views
0

我需要通過發送發送json對象,我無法讓它工作。我有它,以便它成功返回,但響應是空的,我想不通爲什麼事情我已經嘗試是我需要幫助通過POST發送JSON並獲得響應

new Ajax.Request("http://twittersentiment.appspot.com/api/bulkClassifyJsonRequests", { 
method: "post", 
postBody:JSONstring, 
onSuccess: function(transport){ 
var response = transport.responseText; 
alert("[email protected] \n" + transport.responseText + "no response"); 
}, 
onFailure: function(){alert("try again")} 
}); 

var http = new XMLHttpRequest(); 
http.open("POST","http://twittersentiment.appspot.com/api/bulkClassifyJsonRequests",true); 
http.onreadystatechange = function() { 
if(http.readyState == 4) 
{ 
    if(http.status == 200) 
    { 
     document.write(http.response.data); 
} 
else 
{ 
    alert(http.statusText); 
} 
} 
}; 
http.send(JSONstring); 

回答

0

Same Origin Policy正在阻止該呼叫的發生。您可以在兩者之間安裝代理服務器以進行這些調用,並將輸出返回到您的AJAX腳本。有關更多詳細信息,請參閱Why You Need a Proxy

+1

這是什麼?你只是發佈了與我相同的答案,甚至鏈接到同一篇文章。 – 2011-04-26 14:28:17

+0

34分鐘複製麪食 – 2011-04-26 17:59:33

+0

@Drackir - 有一項服務被稱爲維基百科。還有另一項服務稱爲Google。 Google用於搜索所有內容。維基百科就像一切的信息存儲。當您在Google中搜索某些內容時,大多數時候顯示的第一個鏈接都來自Wikipedia。因此,相同的鏈接。順便說一下,你的一個upvotes是我的。別客氣! – Anurag 2011-04-26 18:07:56

3

的問題是,你想送跨域請求(它違反了same origin policy)。這是一個安全問題,瀏覽器不允許。如果twittersentiment.appspot.com提供了JSONP選項,則可以使用該選項。否則,你將不得不通過你的網站或類似的想法進行代理。

編輯
注意:這僅適用於使用AJAX。此外,看着他們的api docs,他們似乎確實支持JSONP的「分類服務」。也許你可以將它與「批量分類服務(JSON)」一起使用?