简体   繁体   English

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

[英]I need help sending JSON via POST and getting a response

I need to send a json object via post and i cannot get it working. 我需要通过发布发送一个json对象,但我无法正常工作。 I have it so that it returns successfully but the response is empty and i cannot figure out why things i have tried are 我有它,以便它成功返回,但响应为空,我无法弄清为什么我尝试过的事情是

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")}
});

and

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);

The problem is that you're trying to send a cross-domain request (it violates the same origin policy ). 问题是您正在尝试发送跨域请求(它违反了相同的原始策略 )。 This is a security issue and is not allowed by browsers. 这是一个安全问题,浏览器不允许这样做。 If twittersentiment.appspot.com provides a JSONP option, you can utilize that instead. 如果twittersentiment.appspot.com提供了JSONP选项,则可以使用该选项。 Otherwise, you'll have to look into proxying through your website or similar ideas. 否则,您将不得不通过您的网站或类似的想法来研究代理。

Edit 编辑
Note: This only applies to using AJAX. 注意:这仅适用于使用AJAX。 Also, After looking at their api docs , it seems they do support JSONP for their "Classification Service". 另外,在查看他们的api文档之后 ,看来他们确实为他们的“分类服务”支持JSONP。 Perhaps you can use it with the "Bulk Classification Service (JSON)" as well? 也许您也可以将其与“大分类服务(JSON)”一起使用?

The Same Origin Policy is preventing that call from happening. 相同来源策略阻止了该调用的发生。 You could install a proxy server in between to make those calls, and return the output to your AJAX script. 您可以在两者之间安装代理服务器以进行这些调用,然后将输出返回到AJAX脚本。 See Why You Need a Proxy for more details. 有关更多详细信息,请参见为什么需要代理

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM