Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Im fetching JSON data using an AJAX call and theres a variable in the ajax URL that I want to change when a user types in a new variable in an input box.
E.g. it starts as www.'AAPL'.com but if a user types 'GOOG' then the variable in the url will change so the new url is www.GOOG.com

I have tried using JSONP to make this work but I cannot get the URL to change so that the new data is retrieved.

Using alert statements I see that the getTicker function is ok, then the ajaxCall function alerts the same value but the actual AJAX call is where it all fails.

Any suggestions?

JavaScript
	var closePrices = new Array();
	var dateArray = new Array();
	var timeStampArray = new Array();
	var timeClose = new Array();
   
	
function jsonCallback(data, ticker) {
			alert('JSON call: ' + 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 ajaxCall(ticker) {  
    alert('ajax call: ' + ticker);
    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){
			alert("success: " + ticker);
			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() {
	    ticker = document.getElementById('userInput').value;
	    alert('user input: ' + ticker);
	    ajaxCall(ticker);
	}
Posted
Updated 9-Apr-13 6:34am
v2

1 solution

Hello,

Since this is a continuation of your last question[^] I am putting up a small snippet to show how this can be done (Assuming parameter ticker represents value you want to replace.
JavaScript
function createChart(ticker) {
    $.support.cors = true;
    //Ajax call retrieves the data from Yahoo! Finance API
    $.ajax({
	type: 'GET',
	async: false,
	dataType: "json",
	url: 'http://query.yahooapis.com/v1/public/yql',
	data: { q:'SELECT * FROM yahoo.finance.historicaldata WHERE symbol="' + ticker + '" AND startDate="2013-01-01" AND endDate="2013-04-3"',
	format:'json', diagnostics:true, env:'store://datatables.org/alltableswithkeys'},
	crossDomain:true,
	success: function (data, status, jqXHR) {
	    renderChart(data, status, jqXHR);
	},
	error: function (jqXHR, status, error) {
	    console.log('Error: ' + error);
	}
    });
}

Regards,
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900