簡體   English   中英

在jive api錯誤上構建股票行情掛件

[英]building a stock ticker widget on jive api error

我向使用HTML小部件的客戶的Jive網站添加了一個實時股票小部件。 該代碼使用Jive本機jQuery庫使用其YQL API從Yahoo提取JSONP數據。 編寫此小部件僅可使用1股票代碼。 如果需要,我可以對其進行修改以引入多個符號。

我不斷收到錯誤的if (res.query.results) 它說它是未定義的。 如果有人知道一個帶有價格和符號名稱的優質HTML HTML小部件:請幫助。 我正在深入研究Google。

控制台輸出:

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>

Yahoo Finance APi已於今年早些時候關閉,包括通過YQL查詢訪問此信息。 您將需要查找替代服務並相應地更新我們的代碼。

關於此的另一個主題是Yahoo Finance API更改(2017)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM