Retrieving stock updates using Yahoo Finance

佐手、 提交于 2019-12-03 09:01:04

You have several mistakes in your JS code:

  1. Incorrect selector for a button: $('getupdate') => $('#getupdate');
  2. Wrong quotes inside url value;
  3. Wrong query string to Yahoo API;
  4. Wrong comment sign \\For Example:FB.

Your JS should be like this:

jQuery(document).ready(function($){
    $('#getupdate').click(function() {
            var symbol = $('input[id=symb]').val(); 
            var url = 'http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.quotes%20where%20symbol%3D%22' + symbol + '%22&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=';
            $.getJSON(url, function(data) {
                var items = [];
                $('#results').html('');
                $.each(data.query.results.quote, function(key, val) {
                    items.push('<li id="' + key + '">' + val + '</li>');

                });
                $('<ul/>', { 'class': 'my-new-list', html: items.join('')}).appendTo('#results');
            });
    });
});

And please add this code after <button> tag in your HTML. This will help you easily clear results before new query:

<div id="results"></div>

Here is an example: http://jsfiddle.net/6EFqk/1/

I'm not shure that ouput format is correct, please reformat it as you like.

First, you aren't selecting the button correctly.

$('#getupdate').click(function() {

Secondly, your slashes are backwards for a comment, they are always forward not back.

//For Example:FB

And finally, you aren't getting any results for this reason given back by the service:

The current table 'yahoo.finance.quotes' has been blocked. It exceeded the allotted quotas of either time or instructions

http://jsfiddle.net/RVFW3/

I just found this stream doing a google search on the subject.

Perhaps this site will answer your questions ... http://bitbenderz.com/stockticker/

Marc

Here is a version of the solution: jsfiddle

$.getJSON(yqlUrl1, function(data){
    $.each(data.query.results.row, function(index, item){

    var element = $('<div></div>');

    element.append('<span>' + item.symbol + '</span> ');
    element.append('<span>' + item.price + '</span> ');

    if (item.change.indexOf('+') > -1) {
        element.append('<span class="stockUp"> ' + item.change + '</span>');
    } else {
        element.append('<span class="stockDown"> ' + item.change + '</span>');
    }

    element.appendTo('#quotes');   
    });
})
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!