2013-04-06 58 views
0

我發佈了這個問題AJAX URL update因爲我認爲我的代碼的問題是AJAX,但我認爲這可能是HighStocks的問題。HighStocks不更新URL

我有一個外部的.js具有這些功能的文件:

//uses AJAX call to retrieve data and then creates the chart with the data 
    function createChart(ticker) { 

     $.ajax({ 
      type: 'post', 
      url: 'http://...' + ticker + '....com', 
      success: function (data, status) { 
      //chart is rendered in here 
      } 

    //gets the user inputted ticker symbol from a HTML input box 
    // and passes to chart function 
    function getTicker() { 
     var ticker = document.getElementById('userInput').value; 
     createChart(ticker); 
    } 

我的HTML文件只是有一個簡單的形式,加上一個輸入框和一個按鈕,點擊時調用getTicker功能。出於某種原因,圖表沒有被創建,並且AJAX調用似乎不起作用。

這可能與HighStocks有關嗎?任何建議,將不勝感激。

UPDATE感謝您的建議,我試圖使用JSONP,但圖表仍然無法加載。任何人都可以看到我做錯了什麼?

 var closePrices = new Array(); 
    var dateArray = new Array(); 
    var timeStampArray = new Array(); 
    var timeClose = new Array(); 

function jsonCallback(data, ticker) { 

      console.log(data); 

      //Put all the closing prices into an array and convert to floats 
      for(var i=0; i < data.query.results.quote.length; i++) 
      { 
       closePrices[i] = parseFloat(data.query.results.quote[i].Close); 
      } 

      //displays the values in the closePrices array 
      console.log(closePrices); 

      //Put all the dates into an array 
      for(var i=0; i < data.query.results.quote.length; i++) 
      { 
       dateArray[i] = data.query.results.quote[i].date; 
      } 

      //Convert all the dates into JS Timestamps 
      for(var i=0; i < dateArray.length; i++) 
      { 
       timeStampArray[i] = new Date(dateArray[i]).getTime(); 
      } 


      for(var i=0; i<data.query.results.quote.length; i++) 
      { 
        timeClose.push([timeStampArray[i], closePrices[i]]); 
      } 

      timeClose = timeClose.reverse(); 
      console.log (timeClose); 

      //displays the dateArray 
      console.log(dateArray); 
      console.log(timeStampArray); 

      // Create the chart 
     $('#container').highcharts('StockChart', { 


      rangeSelector : { 
       selected : 1 
      }, 

      title : { 
       text : ticker + ' Stock Price' 
      }, 

      series : [{ 
       name : ticker, 
       data: timeClose, 
       tooltip: { 
        valueDecimals: 2 
       } 
      }] 
     }); 
} 

function createChart() { 

    var url = 'http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.historicaldata%20where%20symbol%20%3D%20%22' + ticker +'%22%20and%20startDate%20%3D%20%222013-01-01%22%20and%20endDate%20%3D%20%222013-02-25%22&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=?'; 
    //Ajax call retrieves the data from Yahoo! Finance API 
    $.ajax(url, { 
     dataType: "jsonp", 
     success: function(data, status){ 
      console.log(status); 
      jsonCallback(data, ticker); 
     }, 
     error: function(jqXHR, status, error) { 
      console.log('Error: ' + error); 
     } 
    }); 
} 


//Function to get ticker symbol from input box. 
function getTicker() { 
     var ticker = document.getElementById('userInput').value; 
     createChart(ticker); 
    } 
+1

您試圖撤回的數據格式是什麼?這看起來很可能是同源策略的一個問題:http://en.wikipedia.org/wiki/Same_origin_policy - 通常會阻止ajax pull工作,除非您使用JSONP或SCRIPT作爲格式數據。 – 2013-04-06 13:39:16

+0

即時通訊獲取JSON數據,這是否意味着我無法更改AJAX網址來檢索不同的公司數據?這很煩人。 – James 2013-04-06 16:51:29

+1

你需要使用JSONPin這種情況。 – 2013-04-08 11:11:38

回答

0

感謝Jeffrey Blake和Pawel Fus的建議。使用JSONP我能夠讓我的程序正常運行:)