简体   繁体   English

使用jquery $ .ajax function()调用javascript函数

[英]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. 我希望twitter.js文件获取twitterUrl变量并对其进行处理,以获取twitter API生成的json文件。 The questions are: 问题是:

  1. How can I read and parse the twitterUrl in the twitter.js file? 如何读取和解析twitter.js文件中的twitterUrl?
  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? 如何在JavaScript中模拟stream_get_contents PHP函数以解析和处理Twitter链接并将结果传递回原始页面?

1. 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: 2.其中myMethod是要将数据发送到的函数:

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 : 因此,例如myMethod

var myMethod = function(data) {
  alert('Your name is ' + data.user.name);
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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