Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to 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?
 
	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 9-Apr-13 7:04am
Edited 9-Apr-13 7:34am
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
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,
  Permalink  

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



Advertise | Privacy | Mobile
Web04 | 2.8.1411022.1 | Last Updated 9 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100