I have a table with 200 rows. I am trying to access a third party API for IMDB website, which will return response for the title given in the开发者_JAVA百科 below format.
http://www.imdbapi.com/?t=Inception
My table has all titles populated. Now I need to access this URL with 't' parameter being changed dynamically from the database. I have my synchronous java script ready, but How do I call this script in a loop for n rows. Thank you.
I'm assuming you have preloaded the titles into a JavaScript array of the form: ['Title1', 'Title2'] etc. My example below makes use of JQuery to simplify the use of Ajax.
var titles = ['Inception', 'Batman Begins', 'Terminator Salvation'];
for (var i=0;i<titles.length;i++) {
$.getJSON('http://www.imdbapi.com/?t=' + titles[i], function(data) {
// HANDLE THE DATA HERE
});
}
There is one small catch, however. The code is not going to work on most browsers because you are trying to access a site whose domain is different from yours. That is called cross-domain scripting and most browsers do not allow it. This is typically handled by means of a proxy i.e. you get your server to connect to the URL using an Http client and then you access it from your server. JSONP is another approach you might use. This post can be helpful:
Firefox setting to enable cross domain ajax request
精彩评论