user2597504
user2597504

Reputation: 1533

jQuery AJAX from different URLs

I am writing a Javascript program which can query data from more than one servlets each sec. If it queries from one servlet, I know how to do it in xxx.js file:

var TPS_URL = "http://localhost:8888/tps";
var jQueryFunction = function()
                {
                    $.ajax
                            ({
                                type: "GET",
                                async: false,
                                url: TPS_URL,
                                dataType: "jsonp",
                                jsonp: "callback",
                                jsonpCallback: "tpsHandler",
                                success: function(json)
                                {
                                 .......
                                }
                            });
                };
setInterval(jQueryFunction,1000);

But now I have another servlet to query. How can I add another servlet into this js file? Just simply create another "TPS_URL_2" and "jQueryFunction_2", and do the same thing above?

var TPS_URL_2 = "http://localhost:9000/tps";
var jQueryFunction_2 = function()
                {
                    $.ajax
                            ({
                                type: "GET",
                                async: false,
                                url: TPS_URL2,
                                dataType: "jsonp",
                                jsonp: "callback",
                                jsonpCallback: "tpsHandler",
                                success: function(json)
                                {
                                 .......
                                }
                            });
                };
setInterval(jQueryFunction_2,1000);

??

Also, if I get a result from the first url and also another result from the other url, and I want to SUM them together, how can I do it?

Upvotes: 0

Views: 73

Answers (1)

Michael B.
Michael B.

Reputation: 2809

set the URL as a parameter to the function and call this function as many times as you need

var jQueryFunction = function(xurl)
                {
                    $.ajax
                            ({
                                type: "GET",
                                async: false,
                                url: xurl,
                                dataType: "jsonp",
                                jsonp: "callback",
                                jsonpCallback: "tpsHandler",
                                success: function(json)
                                {
                                 .......
                                }
                            });
                };

Call it like this

setInterval( function(){ jQueryFunction('some url') },1000);

Upvotes: 1

Related Questions