2013-06-21 68 views
9

我試圖從雅虎api獲取股票報價。 我對查詢的輸入只是一個股票代碼(來自文本字段)。在按鈕上單擊背景JavaScript方法「getprice()」被調用。 我有一個Java腳本代碼,看起來像這樣使用javascript以json格式獲取雅虎財經的股票報價

function getprice() 
{ 
    var symbol = $('#stockquote').val(); 


    var url = "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.quotes%20where%20symbol%20in%20(%22"+symbol+"%22)%0A%09%09&env=http%3A%2F%2Fdatatables.org%2Falltables.env&format=json"; 

    $.getJSON(url, function (json) 
    { 

     var lastquote = json.query.results.quote.LastTradePriceOnly; 
     $('#stock').text(lastquote); 

    }); 
} 

$('#stock').text(lastquote); 

這裏的「股票」是文本字段,我想顯示LastTradePriceOnly對於給定的股票。

我沒有看到任何輸出。 調試也不會顯示任何錯誤。 我能否就此問題得到任何建議?

+0

除非你主持你的yahooapis.com域的JavaScript,你將無法得到JSON由於產地限制。 –

+0

你也可以嘗試使用提供股市API的庫,所以你不必自己實現它。看看[stocks.js](https://github.com/wagenaartje/stocks.js) –

回答

13

試試這個。

function getData() { 
    var url = 'http://query.yahooapis.com/v1/public/yql'; 
    var symbol = $("#symbol").val(); 
    var data = encodeURIComponent("select * from yahoo.finance.quotes where symbol in ('" + symbol + "')"); 

    $.getJSON(url, 'q=' + data + "&format=json&diagnostics=true&env=http://datatables.org/alltables.env") 
     .done(function (data) { 
      $('#result').text("Price: " + data.query.results.quote.LastTradePriceOnly); 
     }) 
     .fail(function (jqxhr, textStatus, error) { 
      var err = textStatus + ", " + error; 
      console.log('Request failed: ' + err); 
     }); 
} 

Here我還爲您添加了工作示例。

+0

感謝您的示例 - 非常好。 –

+0

@vladbezden你將如何實現這個查詢使用多個符號? – rambossa

+1

@MichaelRamos,你可以使用相同的代碼來獲取多個符號。只需發送以逗號分隔的符號列表('MSFT,IBM')進行查詢,現在您的data.query.results.quote對象將具有兩個對象信息,一個用於MSFT,另一個用於IBM。你可以通過data.query.results.quote找到該對象的安全性。[0] .symbol將返回'MSFT'和data.query.results.quote [1] .symbol將返回'IBM' –

3

這是它是如何在情況下AngularJS做你需要它:

在你看來:

<section ng-controller='StockQuote'> 
    <span>Last Quote: {{lang}}, {{lastTradeDate}}, {{lastTradeTime}}, {{lastTradePriceOnly}}</span> 
</section><br> 

在你的控制器:股票符號名通過$ scope.ticker_name服務方法中傳遞'getData.getStockQuote'。

appModule.controller('StockQuote', ['$scope', 'getData', 
function($scope, getData) { 
    var api = getData.getStockQuote($scope.ticker_name); 
    var data = api.get({symbol:$scope.ticker_name}, function() { 
     var quote = data.query.results.quote; 
     $scope.lang = data.query.lang; 
     $scope.lastTradeDate = quote.LastTradeDate; 
     $scope.lastTradeTime = quote.LastTradeTime; 
     $scope.lastTradePriceOnly = quote.LastTradePriceOnly; 
    }); 
}]); 

在服務:

appModule.service('getData', ['$http', '$resource', function($http, $resource) { 
    // This service method is not used in this example. 
    this.getJSON = function(filename) { 
     return $http.get(filename); 
    }; 
    // The complete url is from https://developer.yahoo.com/yql/. 
    this.getStockQuote = function(ticker) { 
     var url = 'http://query.yahooapis.com/v1/public/yql'; 
     var data = encodeURIComponent(
      "select * from yahoo.finance.quotes where symbol in ('" + ticker + "')"); 
     url += '?q=' + data + '&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys'; 
     return $resource(url); 
    } 
}]); 
相關問題