0

我试图使用Twitter API使用JavaScript发布推文。下面Twitter API错误:'内部服务器错误'

基本字符串详细

POST&http%3A%2F%2Fapi.twitter.com%2F1%2Fstatuses%2Fupdate.json&oauth_consumer_key%3DXXXXXXXXXXX%26oauth_nonce%3D9acc2f75c97622d1d2b4c4fb4124632b1273b0e0%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1305227053%26oauth_token%3D159970118-XXXXXXXXXXXXXXXXXXXXXXX%26oauth_version%3D1.0%26status%3DHello

OAuth oauth_nonce="9acc2f75c97622d1d2b4c4fb4124632b1273b0e0", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1305227053", oauth_consumer_key="XXXXXXXXXXXXXXXXX", oauth_token="159970118-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", oauth_signature="IWuyoPJBrfY03Hg5QJhDRtPoaDs%3D", oauth_version="1.0"

我用身体POST方法 “状态=你好”

但我得到一个内部服务器错误。我身边有什么错误?提前致谢。使用

Javascript代码

h是上述

鸣叫给出的标题= “你好”

encodeURLall是用户定义的,其在所有其他场合工作。

var xhr = new XMLHttpRequest(); 
xhr.open("POST","http://api.twitter.com/1/statuses/update.json", false); 
xhr.setRequestHeader("Authorization",h); 

xhr.onreadystatechange = function() { 

    if (xhr.readyState == 4) 
    { 
     console.log("STATUS="+xhr.status); 
     console.log("RESPONSE="+xhr.responseText); 
    } 
} 

xhr.send("status="+encodeURLall(tweet)); 

} 
+0

你是从浏览器发布的?或者有没有服务器端代码做这个职位? – jimbojw 2011-05-12 19:30:13

+0

你能告诉我们你使用的代码吗? – 2011-05-12 19:33:29

+0

@jimbojw我只从浏览器发布。 – Ram 2011-05-14 05:05:30

回答

0

由于Same origin policy,您无法使用XMLHttpRequest访问Twitter网站。改为使用JSONP或使用服务器端代理(调用您自己的服务器,将您的请求重定向到Twitter)。

顺便说一下,encodeURLall()做什么?你不应该只用encodeURIComponent


更新:引述Google

Regular web pages can use the XMLHttpRequest object to send and receive data from remote servers, but they're limited by the same origin policy . Extensions aren't so limited. An extension can talk to remote servers outside of its origin, as long as it first requests cross-origin permissions.

请仔细阅读那里看看哪些设置,你应该为了使这项工作发生改变。

+0

所以我应该做这样的'$阿贾克斯({ \t \t \t \t \t 'URL': 'http://api.twitter.com/1/statuses/update.json', \t \t \t \t \t '数据':{状态:encodeURLall(鸣叫)}, \t \t \t \t \t '类型': '后', \t \t \t \t \t '成功':onUpdateCallback, \t \t \t \t \t '的dataType': 'JSONP' \t \t \t \t});'但如何将我设置请求头。 – Ram 2011-05-14 12:10:21

+0

顺便说一句我正在尝试创建一个谷歌浏览器扩展。只是想给你清晰的想法,我正在做的扫管笏。那么是不是可以在jsonp中设置标题? – Ram 2011-05-14 12:19:56

+0

@Ram:在Chrome扩展中,它可能会有所不同。 – 2011-05-14 12:55:54