Štěpán Heller
Štěpán Heller

Reputation: 151

Retrieving user_timeline from Twitter in XML

I'm writing a simple app in HTML and Javascript. I'm trying to retrieve my user_timeline via jQuery's .ajax() method. The problem is that I want to retrieve my timeline in XML but I'm keep failing with this simple task. Here's my code:

$.ajax({
        type: 'GET',
        dataType: 'xml',
        url: 'http://api.twitter.com/1/statuses/user_timeline.xml?screen_name=stepanheller',
        success: function(data, textStatus, XMLHttpRequest) {
            console.log(data);
        },
        error: function(req, textStatus, error) {
            console.log('error: '+textStatus);
        }

});

Weird thing is that when I try exactly the same thing but with JSON instead of XML then the script works.

$.ajax({
        type: 'GET',
        dataType: 'jsonp',
        url: 'http://api.twitter.com/1/statuses/user_timeline.json?screen_name=stepanheller',
        success: function(data, textStatus, XMLHttpRequest) {
            console.log(data);
        },
        error: function(req, textStatus, error) {
            console.log('error: '+textStatus);
        }

});

Can you give me some hints what I'm doing wrong with the request? I know that I'm using old version of API but I won't deal with OAuth right now. Thanks.

Upvotes: 0

Views: 412

Answers (1)

Julien Lafont
Julien Lafont

Reputation: 7877

It is generally impossible to send a Cross-domain ajax request. It's the general rule.

JsonP is the classic way to work around this limitation, and there is no equivalent for Xml according to my knowledge. Depending on your browser compatibility constraints, you can use XHR2 to achieve this.

Otherwise, the only solution is to set up a server proxy.

Client --Ajax--> Your server --HttpRequest--> Twitter

Upvotes: 1

Related Questions