I'm trying to make AJAX on the Twitter Rast API. I talked down, but nothing happens.
The server did not receive any response (Twitter) Do I miss something?
function getRecentTweet () {twitterUsername = document.getElementById ("twitterUsername") value; If (window.XMLHttpRequest) {xhr = New XMLHttpRequest (); } And {xhr = new ActiveXObject ("Microsoft.XMLHTTP"); } Xhr.open ("GET", "http://api.twitter.com/1/statuses/user_timeline/" + name of Twitter name + ".Jason", is true); Xhr.send (zero); Xhr.onreadystatechange = function () {if (xhr.readyState == 4) {if (xhr.status == 200) {var jsonObj = eval (responseJSON); Document.getElementById ("twitterstream"). InnerHTML = jsonObj [0] .text; }}}} Thanks!
Yes, cross-domain AJAX calls are not allowed You need to use JSONP.
Request Twitter's API and attach the callback parameter. Here is a sample url:
In response to Twitter you can insert executable JavaScript code in the name of a function named as a callback parameter. So Twitter's response looks like this:
myFunction (& lt; JSON here & gt;); Since cross-domain is a problem, you have to add a script tag to the page, and set the src attribute on the above URL. It can be dynamically created and injected in the page with Javascript.
& lt; Script src = "... hulu.json? Callback = myfunction" & gt; & Lt; / Script & gt; Then define global function myfunction on the page that whenever it responds to twitter returns it can also be predefined or dynamically generated.
& lt; Script & gt; Function myFunction (data) {console.log (data); } & Lt; / Script & gt;
Comments
Post a Comment