我需要帮助通过POST发送JSON并获得响应

I need help sending JSON via POST and getting a response

本文关键字:JSON 响应 发送 POST 帮助      更新时间:2023-09-26

我需要通过post发送一个json对象,我不能让它工作。我有它,所以它成功返回,但响应是空的,我不能弄清楚为什么我尝试的东西是

new Ajax.Request("http://twittersentiment.appspot.com/api/bulkClassifyJsonRequests", {
method: "post",
postBody:JSONstring,
onSuccess: function(transport){
var response = transport.responseText;
alert("Success@ '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);

问题是您正在尝试发送跨域请求(它违反了同源策略)。这是一个安全问题,浏览器不允许这样做。如果twittersentiment.appspot.com提供了一个JSONP选项,你可以利用它。否则,你将不得不考虑通过你的网站代理或类似的想法。

编辑
注意:这只适用于使用AJAX。而且,在看了他们的api文档之后,他们的"分类服务"似乎确实支持JSONP。也许您也可以将其与"批量分类服务(JSON)"一起使用?

同源策略阻止该调用发生。您可以在两者之间安装一个代理服务器来进行这些调用,并将输出返回给AJAX脚本。