Created
January 14, 2017 04:46
-
-
Save critesjosh/a94b08d30f74ef8bb779aaf65740629d to your computer and use it in GitHub Desktop.
My API call from my stock exchange app
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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