Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does my API call work in chrome but not in my code?

I'm trying to call the Binance API to get the LTC price in BTC and I tested the link on my browser "https://api.binance.com/api/v1/ticker/price?symbol=LTCBTC" How do i get the json file from that link into my javascript file?

$(document).ready(function() {

var url = 'https://api.binance.com/api/v1/ticker/price?symbol=LTCBTC';

$.ajax( {
    url: url,
    dataType: 'jsonp',
    type: 'GET',
    success: function(data) {
            console.log(data); //returns nothing
        }
});

})
like image 250
crypto virtex Avatar asked Apr 20 '19 19:04

crypto virtex


1 Answers

As mentioned in other answer, there is CORS issue. So you can try with proxyURL from client side as below,

$(document).ready(function() {

var url = 'https://api.binance.com/api/v1/ticker/price?symbol=LTCBTC';

const proxyURL = "https://cors-anywhere.herokuapp.com/";
$.getJSON(proxyURL + url, function(playerData) {
  console.log(playerData);
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

Hope it helps.

like image 99
Vikas Yadav Avatar answered Oct 04 '22 00:10

Vikas Yadav