Hello,
Hello,
Generally your URL will be fix, what's going to change is the data you will be sending, which can either be sent as query string parameters or as POST request parameters. Since you are using POST request, your code should look something like
function createChart(ticker) {
$.ajax({
type: 'post',
url: 'http://your chart service url',
data: {"chartData": ticker},
success: function (data, status) {
}
});
function getTicker() {
var ticker = document.getElementById('userInput').value;
createChart(ticker);
}
For more information please have a look at
JQuery documentation.
Working sample
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="X-UA-Compatible" content="IE=Edge"></meta>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"></meta>
<meta name="author" content="Prasad P. Khandekar"></meta>
<meta name="owner" content="Fundtech INDIA Ltd."></meta>
<meta name="copyright" content="Fundtech INDIA Ltd."></meta>
<meta http-equiv="Expires" content="0"></meta>
<meta http-equiv="Pragma" content="no-cache"></meta>
<meta http-equiv="Cache-Control" content="no-cache"></meta>
<meta http-equiv="Pragma-directive" content="no-cache"></meta>
<meta http-equiv="cache-directive" content="no-cache"></meta>
<script type="text/javascript" src="js/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="js/highstock.js"></script>
<script type="text/javascript" src="js/highcharts-more.js"></script>
<script type="text/javascript" src="js/modules/exporting.js"></script>
<script type="text/javascript">
var closePrices = new Array();
var dateArray = new Array();
var timeStampArray = new Array();
var timeClose = new Array();
var ticker = "Stock Quotes";
function renderChart(data, status, jqXHR) {
console.log(status);
console.log(data);
for (var i = 0; i < data.query.results.quote.length; i++) {
closePrices[i] = parseFloat(data.query.results.quote[i].Close);
}
console.log(closePrices);
for (var i = 0; i < data.query.results.quote.length; i++) {
dateArray[i] = data.query.results.quote[i].date;
}
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);
console.log(dateArray);
console.log(timeStampArray);
$('#container').highcharts('StockChart', {
rangeSelector: {
selected: 1
},
title: {
text: 'AAPL Stock Price'
},
series: [{
name: ticker,
data: closePrices
}]
});
}
function createChart(ticker) {
$.support.cors = true;
$.ajax({
type: 'GET',
async: false,
dataType: "json",
url: 'http://query.yahooapis.com/v1/public/yql',
data: { q:'SELECT * FROM yahoo.finance.historicaldata WHERE symbol="MSFT" 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);
}
});
}
function getTicker() {
ticker = document.getElementById('userInput').value;
createChart(ticker);
return false;
}
</script>
</head>
<body>
<form id="form" method="POST">
<h3>Enter the ticker symbol of the company you wish to view:</h3>
<input type="text" id="userInput">
<button type="button" onclick="getTicker();">Get Chart</button>
</form>
<div id="container" style="height: 500px; min-width: 500px"> </div>
</body>
</html>
Regards,