Consuming Json, problem with Cors

Asked

Viewed 129 times

0

Good morning!

I am performing a query (via jQuery and Json) in the Mercadobitcoin API, but always get an error in return. When inspecting, it is said that there is a fault by the CORS method. I checked the documentation, there’s a "methods have CORS" warning. I looked in several places but still not found how to solve the query without needing something on the server side.

Doc from the API: https://www.mercadobitcoin.com.br/api-doc/

I’m using a simple query like:

$.get( "http://meusite.com/", function( data )

Someone could give me a light?

Hugs.

2 answers

0

Here is a functional example (check the console) of using the API: https://pastebin.com/xpr8DnMB

According to the DOC, the URL is this: https://www.mercadobitcoin.net/api/<coin>/<method>/, where Coin can be BTC, LTC or BCH; where method can be Ticker, orderbook or Trades

0

If the methods have CORS enabled in server side a small change in your AJAX request can solve everything, you must add the line crossDomain: true in your request header.

Here is an example of an ajax request using CORS true in the header:

$.ajax({
        url: "http://meusite.com/",
        type: "GET",
        crossDomain: true, 
        dataType: "json",
        success: function (response) {
            var resp = JSON.parse(response)
            alert(resp.status);
        },
        error: function (xhr, status) {
            alert("error");
        }
    });

Browser other questions tagged

You are not signed in. Login or sign up in order to post.