/    Sign up×
Community /Pin to ProfileBookmark

How does twitter work in ajax if it’s a diff. domain?

From what I know, one major limit of ajax in a web browser is that you cannot use ajax to talk to a different domain from which your page was served. Yet, if I do an ajax request for the JSON object at twitter.com (for example “http://twitter.com/statuses/user_timeline.json“) it works! Why is this?

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@JonaSep 01.2010 — [font=arial]Hi,

The JSON format (dangerously) allows for cross-domain Ajax requests. This type of request is known as JSONP (JSON with padding).

The reason it works is because it creates a SCRIPT tag that points to the external site (Twitter, in this case). This only works with the JSON format because it JSON is also valid JavaScript. Since there are no domain restrictions on the SCRIPT tag (you can set the SRC attribute to any external domain), the browser does not block the creation of the SCRIPT tag; and since it's not invalid JavaScript, the values come in as JavaScript variables directly. Make sense? ;-) [/font]
Copy linkTweet thisAlerts:
@6tr6trauthorSep 01.2010 — [font=arial]Hi,

The JSON format (dangerously) allows for cross-domain Ajax requests. This type of request is known as JSONP (JSON with padding).

The reason it works is because it creates a SCRIPT tag that points to the external site (Twitter, in this case). This only works with the JSON format because it JSON is also valid JavaScript. Since there are no domain restrictions on the SCRIPT tag (you can set the SRC attribute to any external domain), the browser does not block the creation of the SCRIPT tag; and since it's not invalid JavaScript, the values come in as JavaScript variables directly. Make sense? ;-) [/font][/QUOTE]


Wow, I can see that being a huge security hole.
Copy linkTweet thisAlerts:
@JonaSep 01.2010 — [font=arial]Generally speaking, the primary problem with circumventing the same origin policy is verifying trusted sources. If you know, absolutely, that the JSON data you're importing is from a trusted source, then you are completely safe and secure. However, if there's even a remote possibility that your source is untrusted (i.e., dynamic source URI's), then you could potentially have a very big and very dangerous security problem.[/font]
Copy linkTweet thisAlerts:
@rnd_meSep 01.2010 — It's safer to use XMLHttpRequest to grap the JSON whole (as a string), and use JSON.parse() to turn it into an object. If you add the CORS headers to your API page, modern browsers can fetch your data via ajax from any domain.
×

Success!

Help @6tr6tr spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.22,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...