views:

489

answers:

2

Hi,

I am attempting to use XMLHTTPRequest to get an update on twitter.

var XMLReq = new XMLHttpRequest();
XMLReq.open("GET", "http://twitter.com/account/verify_credentials.json", false, "TestAct", "password");
XMLReq.send(null);

However, using my sniffer I cannot see any authorization headers being passed through. Hence, I get a 401 error response from Twitter.

The account and password are correctly entered.

Anyone attempt this? Can anyone give me some pointers? Thank you.

A: 

Use a server-side proxy

Josh Stodola
Why isn't what I am doing working? And what is server-side proxy?
x1a0
I'd say it is not working because XMLHTTPrequest does not fully support authentication, at least not with GET requests. A proxy will be the "middle-man" between your app and Twitter. You pass it your credentials, it performs the authenticated request to Twitter, and passes the response back. This is a way bypass the cross-domain request restriction as well.
Josh Stodola
How do I go around setting up a server-side proxy? Are there any links online you can point me to? Thanks.
x1a0
Im working on my local machine btw.
x1a0
Well I managed to make it work. I sent in a header with the authorization...XMLReq.setRequestHeader("Authorization", "Basic " + Base64Encode("Acct:pass"));
x1a0
Post that as an answer and accept it, please.
Josh Stodola
A: 

Due to the Origin policy, you cannot make a XMLHttpRequest from your domain to another domain. E.g. you cannot use http://twitter.com/... URLs unless your script was loaded from twitter.com. If your script is loaded from http://localhost/, the AJAX request also need to go to localhost.

mhaller