简体   繁体   中英

Using jquery $.ajax function() to call a javascript function

I have the following code:

var twitterUrl = escape("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:

  1. How can I read and parse the twitterUrl in the twitter.js file?
  2. 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);
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM