Using jquery $.ajax function() to call a javascript function
I have the following code:
var twitterUrl = esc开发者_运维问答ape("http://api.twitter.com/1/statuses/user_timeline.json?screen_name=" + username);
$.ajax({
type: "GET",
url: "twitter.js?url=" + twitterUrl,
dataType: "json",
success: function(d, status, req) {
var tweets = eval('(' + d + ')');
showTweets(tweets);
}
});
I want the twitter.js
file to get the twitterUrl variable and process it to obtain the json file generatd by the twitter API. The questions are:
- How can I read and parse the twitterUrl in the twitter.js file?
- How can I simulate a
stream_get_contents
PHP function in JavaScript to parse and process the twitter link and pass the result back to the original page?
1.
var twitterUrl = location.search.match(/[?&]url=(.*?)(&|$)/i);
twitterUrl = twitterUrl && twitterUrl[1];
2.
Where myMethod
is the function you want to send the data to:
var script = document.createElement('script');
script.src = twitterUrl + '&callback=myMethod';
var head = document.getElementsByTagName('head')[0];
head.insertBefore(script, head.firstChild);
So for example myMethod
:
var myMethod = function(data) {
alert('Your name is ' + data.user.name);
};
精彩评论