Skip to content

Instantly share code, notes, and snippets.

@critesjosh
Created January 14, 2017 04:46
Show Gist options
  • Save critesjosh/a94b08d30f74ef8bb779aaf65740629d to your computer and use it in GitHub Desktop.
Save critesjosh/a94b08d30f74ef8bb779aaf65740629d to your computer and use it in GitHub Desktop.
My API call from my stock exchange app
function apiCall(currentSymbol) {
var searchTerm;
if (currentSymbol) {
searchTerm = currentSymbol;
} else {
searchTerm = $('#searchTerm').val();
searchTerm = searchTerm.toUpperCase();
}
var url = "http://data.benzinga.com/rest/richquoteDelayed?symbols=" + searchTerm;
$('.loaderImage').show();
$('#searchResult').hide();
$('#portfolio').hide();
$('#startOver').hide();
$.ajax({
url: url,
method: 'GET',
crossDomain: true,
dataType: "jsonp",
headers: {
'Access-Control-Allow-Origin': 'https://dreamcatcherproject.net'
},
timeout: 8000,
error: function error(jqXHR, textStatus, errorThrown) {
if (textStatus === "timeout") {
alert("connection timed out");
location.reload();
} else {
alert("An error occured, please try again.");
}
}
}).done(function (result) {
var title = result[searchTerm].name;
var symbol = result[searchTerm].dxSymbol;
var bidPrice = result[searchTerm].bidPrice;
var askPrice = result[searchTerm].askPrice;
var header = title + ' (' + symbol + ')';
//display search results
$('.loaderImage').hide();
$('#searchResult').show();
$('#portfolio').show();
$('#startOver').show();
$('#title').html(header);
$('#bid').html('$ ' + bidPrice);
$('#ask').html('$ ' + askPrice);
//save search results in the data model
model.currentStock.name = title;
model.currentStock.symbol = symbol;
model.currentStock.bidPrice = bidPrice;
model.currentStock.askPrice = askPrice;
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment