Yash Parekh
Yash Parekh

Reputation: 129

json api get request error

The following is my code:

$(document).ready(function(){
        $.ajax({
            url: 'https://bitconnect.co/api/info/BTC_BCC',
            type: 'get',
            dataType: 'json',
            success: function(data){
                alert(data);
            },
            error: function(error){
                alert(error);
            }
        });
    });
<html>
        <head>
            <meta charset="UTF-8">
            <title></title>
            <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
            <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
            <script src = "//code.jquery.com/jquery-1.12.4.js"></script>
    
            <script src="try.js"></script>
        </head>
        <body>
            
        </body>
    </html>

I want to get the information from the url mentioned in the url section of ajax. But I'm getting the following error :

Failed to load https://bitconnect.co/api/info/BTC_BCC: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access.

I'm completely new to this section and have no idea of what the error is. It would be great if I could get any kind of help. Thanks in advance.

Upvotes: 0

Views: 78

Answers (2)

Argus Malware
Argus Malware

Reputation: 787

you can use jsonp for cross domain request

$.ajax({
   type: 'GET',
    url: 'https://bitconnect.co/api/info/BTC_BCC',
    async: false,
    jsonpCallback: 'jQuery16206304910685867071_1380876031038',
    contentType: "application/json",
    dataType: 'jsonp',
    success: function(json) {
       console.dir(json.PRList);
    },
    error: function(e) {
       console.log(e.message);
    }
});

Upvotes: 0

Sagar
Sagar

Reputation: 483

You can do this in this way if you are doing it from localhost and using the proxy server of this app , or you can also self host and create a proxy server by following this url https://github.com/Rob--W/cors-anywhere/

var proxyUrl = 'https://cors-anywhere.herokuapp.com/'

$.ajax({
    url: proxyUrl+'https://bitconnect.co/api/info/BTC_BCC',
    type: 'get',
    dataType: 'json',
    crossDomain: true,
    headers: { "Access-Control-Allow-Origin": "*" },
}).done(function(data) {
    console.log(data);
});

Upvotes: 2

Related Questions