在jive api错误上构建股票行情小工具

时间:2017-11-29 18:33:43

标签: api widget yahoo finance jive

我将一个实时库存小部件添加到使用HTML小部件的客户端Jive网站。该代码使用Jive原生jQuery库,使用JSONP从Yahoo中提取YQL API数据。编写此小部件仅使用1股票代码。如果需要,我可以修改它以引入多个符号。

我一直收到if (res.query.results)的错误。它说这是未定义的。如果有人知道一个好的股票HTML小部件只有价格和符号名称:请帮助。我正深入挖掘谷歌。

控制台输出:

render-widget.jspa?size=1&frameID=262901&widgetType=7&containerID=1327&containerType=700&inFrame=1:173 Uncaught TypeError: Cannot read property 'results' of undefined
at Object.success (render-widget.jspa?size=1&frameID=262901&widgetType=7&containerID=1327&containerType=700&inFrame=1:173)
at j (jquery.min.js:2)
at Object.fireWith [as resolveWith] (jquery.min.js:2)
at x (jquery.min.js:4)
at HTMLScriptElement.b.onload.b.onreadystatechange (jquery.min.js:4)

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
// This version has been tested to work in Jive 4.0.15 and 5.0. It should work in Jive 4.5 but has not been tested
// Add the stock symbol here
  var yourStockSymbol = 'LIFE';
</script>

<div id="stock_miniQuote_head" class="ajaxtrigger"><span id="stockSymbol"></span>&nbsp;(common stock)</div>

<div id="stock_miniQuote">
<div id="stockIndicator"><p>Retrieving stock information...</p></div>


    <div class="stock_divider">

      <div id="stock_left">
        <span class="stock_label">Price</span><br/>
        <strong class="stock_strong">$<span id="stockAsk"></span></strong><br/>
      </div>


      <div id="stock_right">
        <span class="stock_label">Change</span><br/>
        <strong class="stock_strong"><span id="stockChange"></span></strong><br />
        <strong class="stock_strong"><span id="stockChangePercent"></span></strong><br />
      </div>
      <div style="clear: both;"></div>


    </div>

      <div id="stock_body">

      <div id="stock_body_content">
        <span class="stock_label">Volume</span><br/>
        <strong class="stock_strong"><span id="stockVolume"></span></strong>
        <br /><br />
        <span class="stock_label">Average Daily Volume</span><br/>
        <strong class="stock_strong"><span id="stockAvgVolume"></span></strong>
        <br /><br />
        <span class="stock_label">52 Week Range</span><br/>
        <strong class="stock_strong"><span id="stockRange"></span></strong>       

      </div>

      <div style="clear: both;"></div>

    </div>



</div>  



<style>

#stockIndicator { 
  text-align:left;
  padding: 10px;
  margin: 5px;
  color: red;
}

.ajaxtrigger:hover {
  cursor: pointer; 
  cursor: hand;
}

#stock_miniQuote_head {
  background-color:#464A55;
  color:#FFFFFF;
  font-size:14px;
  font-weight:bold;
  padding-bottom:10px;
  padding-left:10px;
  padding-right:10px;
  padding-top:10px;
}

#stock_miniQuote {
  border-bottom-color:#DDDDDD;
  border-bottom-left-radius:5px 5px;
  border-bottom-right-radius:5px 5px;
  border-bottom-style:solid;
  border-bottom-width:1px;
  border-left-color:#DDDDDD;
  border-left-style:solid;
  border-left-width:1px;
  border-right-color:#DDDDDD;
  border-right-style:solid;
  border-right-width:1px;
  border-top-color:initial;
  border-top-style:none;
  border-top-width:initial;
  list-style-type:none;
  margin-bottom:10px;
  padding-bottom:0;
  padding-top:10px;
  vertical-align:text-top;
  height: 100%;
  width: 99%;
}

.stock_divider {
  border-bottom:1px solid #B2B0AD; padding-bottom:5px;
}

#stock_left {
  float:left; width:35%; height:50px; border-right:1px solid #B2B0AD; padding:0 15px;
}

#stock_right {
  float:right; width:*; padding:0 20px; vertical-align:text-top;
}

.stock_label {
  font-size:14px;
}

.stock_strong {
  font-size:17px;
}

#stock_body {
  padding:10px 0 15px;
}

#stock_body_content {
  float:left; width:170px; padding:0 15px;
}

</style>


<script type="text/javascript">

if ($('#jive-widgets-browser').css('display') == 'block') {
// Do Nothing as we are in edit mode

} else {
// Build the URL to Yahoo YQL services
var q = escape('select * from yahoo.finance.quotes where symbol in ("' + yourStockSymbol + '")');
var theURL = "https://query.yahooapis.com/v1/public/yql?q=" + q + "&format=json&diagnostics=false&env=http%3A%2F%2Fdatatables.org%2Falltables.env&callback=?";


$(document).ready(function(){
// Load function on launch
  $("#stockIndicator").show();
  doAjax(theURL);

// Function for refreshing the stock by clicking on the title header
$('.ajaxtrigger').click(function(){
  $("#stockIndicator").show();
  doAjax(theURL);
    return false;
  });

// Function to add commas to numbers for volume
  function numberWithCommas(x) {
    return x.toString().replace(/\B(?=(?:\d{3})+(?!\d))/g, ",");
  }

// Main function to make JSON request to Yahoo for stock information
  function doAjax(url){
  $.ajax({
    url: url,
    dataType: 'jsonp',
    success: function(data){
      var s = data.query.results;
          if(s){
      if(s.quote.Change > 0) {
        // Change the change text to green
        $('#stockChange').css({'color': 'green'});
        $('#stockChangePercent').css({'color': 'green'});
      } else {
        // Change the change text to red
        $('#stockChange').css({'color': 'red'});
        $('#stockChangePercent').css({'color': 'red'});
      }

      // This is where we add the JSON values back into the HTML above
      $('#stockSymbol').html(s.quote.symbol);
      $('#stockAsk').html(s.quote.LastTradePriceOnly);
      $('#stockChange').html(s.quote.Change);
      $('#stockChangePercent').html(s.quote.ChangeinPercent);
      $('#stockVolume').html(numberWithCommas(s.quote.Volume));
      $('#stockAvgVolume').html(numberWithCommas(s.quote.AverageDailyVolume));
      $('#stockRange').html(s.quote.YearRange);

      $("#stockIndicator").hide();

          } else {
            var errormsg = '<p>Error: could not load the page.</p>';
      $("#stockIndicator").show();
            $("#stockIndicator").html(errormsg);
          }
        }
      });

  }


  }); //end ready function

} //end first else





</script>

1 个答案:

答案 0 :(得分:0)

Yahoo Finance APi今年早些时候被关闭,包括通过YQL查询访问这些信息。您需要找到备用服务并相应地更新我们的代码。

这里有另一个关于此问题的帖子,Yahoo Finance API changes (2017)