weixin_33726313 2013-04-06 12:58 采纳率: 0%
浏览 16

HighStocks不更新URL

I posted this question AJAX URL update as I thought the problem with my code was with AJAX but I think this could be an issue with HighStocks.

I have an external .js file with these functions:

    //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);
    }

My HTML file just has a simple form with an input box and a button that when clicked calls the getTicker function. For some reason the chart is not being created and the AJAX call doesnt seem to work.

Is this something with HighStocks maybe? Any suggestions would be appreciated.

UPDATE Thank you for the suggestions, I have attempted to use JSONP but the chart still does not load. Can anybody see what I am doing wrong?

        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条回答 默认 最新

  • larry*wei 2013-04-09 22:39
    关注

    Thanks to Jeffrey Blake and Pawel Fus for your suggestions. Using JSONP I was able to get my program functioning correctly :)

    评论

报告相同问题?

悬赏问题

  • ¥60 Python如何后台操作Vmwake虚拟机键鼠
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容