Reputation: 89
I have an endpoint https://api.iextrading.com/1.0/tops?symbols=aapl but when I try to use .getjson with that url I get a 404 error. In the api documentation it mentions that it may be a jsonp request and if so how do I get .getjson to be able to read this call. Thank you in advance.
The code I have tried is...
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js">
</script>
</head>
<body>
<h2>Create Object from JSON String</h2>
<p id="demo"></p>
<script>
$.getJSON('https://api.iextrading.com/1.0/stock/tsla', function(data) {
var obj = JSON.parse(data);
document.getElementById("demo").innerHTML = obj.id;
});
</script>
</body>
</html>
Upvotes: 0
Views: 283
Reputation: 36
The API or remote resource must set the header. You can try
function(xhr) {
xhr.setRequestHeader('X-Test-Header', 'test-value');
}
Upvotes: 2
Reputation: 2152
The URL you are using doesn't match your description's URL, and the URL actually returns a 404.
Using your description's URL works, however getJSON
parses the data so we don't need to do JSON.parse(data);
.
Finally, your data doesn't actually have a id
attribute so that will return undefined.
I have changed it to symbol
which returns AAPL.
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js">
</script>
</head>
<body>
<h2>Create Object from JSON String</h2>
<p id="demo"></p>
<script>
$.getJSON('https://api.iextrading.com/1.0/tops?symbols=aapl', function(data) {
var obj = data[0];
document.getElementById("demo").innerHTML = obj.symbol;
});
</script>
</body>
</html>
Upvotes: 1