如何更改url参数?

时间:2017-12-16 22:16:20

标签: javascript

如何将url中的参数“symbol”逐个更改为数组的每个元素并多次运行该函数?

var symbols = [MSFT, CSCO, FB, AMZN, GOOG];

window.onload = function() {
$.ajax({
    type: "GET",
    url: "https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol=AAPL&interval=1min&apikey=T6UEJETEQRVGDJS9",
    success: function(result){
        stocks = result;
        document.getElementById("myDiv").innerHTML = stocks;

    }
 });
}

谢谢!

5 个答案:

答案 0 :(得分:4)

你必须将AJAX调用分离为独立函数,然后每次使用不同的参数调用该函数。

像这样:

window.onload = function() {
    var symbols = ['MSFT', 'CSCO', 'FB', 'AMZN', 'GOOG'];

    symbols.forEach( symbol => makeAjaxCall(symbol));

}

function makeAjaxCall(param){

$.ajax({
    type: "GET",
    url: "https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol="+ param +"&interval=1min&apikey=T6UEJETEQRVGDJS9",
    success: function(result){
        stocks = result;
        document.getElementById("myDiv").innerHTML = stocks;
    }
 });
}

答案 1 :(得分:2)

在数组上的forEach中执行:

var symbols = ["MSFT", "CSCO", "FB", "AMZN", "GOOG"];

window.onload = function() {
    symbols.forEach(function(sym){
        var url = "https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&" + sym + "=AAPL&interval=1min&apikey=T6UEJETEQRVGDJS9";
        $.ajax({
            type: "GET",
            url: url,
            success: function(result){
                stocks = result;
                document.getElementById("myDiv").innerHTML = stocks;

            }
         });
    }   
})

答案 2 :(得分:1)

我没有包含保证简单的承诺,但您可以再使用一个参数来指定将接收股票价值的DOM元素。

window.onload = function() {
    var symbols = ['MSFT', 'CSCO', 'FB', 'AMZN', 'GOOG'];
    symbols.forEach(symbol => getSymbol(symbol, divId));
}

function getSymbol(param, divId){
  $.ajax({
      type: "GET",
      url: "https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol="+ param +"&interval=1min&apikey=T6UEJETEQRVGDJS9",
      success: function(result){
          stocks = result;
          document.getElementById(divId).innerHTML = stocks;
      }
   });
}

答案 3 :(得分:0)

您可以将其设置为变量:

$  node
> var symbol = 'aallalla'
undefined
> var html = 'http://www.google.com/?symbol='
undefined
> html + symbol
'http://www.google.com/?symbol=aallalla'
> 

这将允许您根据需要进行更改。

答案 4 :(得分:0)

const getSymbol = (symbol) => {
  return new Promise((resolve, reject) => {
    $.ajax({
        type: "GET",
        url: `https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol=${symbol}&interval=1min&apikey=T6UEJETEQRVGDJS9`,
        success: function(result){
            resolve(result);
        }
    });
  });
};

window.onload = function() {
  const symbolPromises = symbols.map((symbol) => {
    return getSymbol(symbol);
  });

  Promise.all(symbolPromises).then((arrayOfResult) => {
    const stocks = result.join('</br>');
    document.getElementById("myDiv").innerHTML = stocks;
  }).catch((err) => {
    console.error(err);
  });       
}

按照这种方法,您可以根据需要调用尽可能多的符号,在promise.all函数中给出结果中的所有符号结果,并且只需将此结果呈现给用户。