0

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.

Yash Parekh
  • 129
  • 1
  • 10
  • Possible duplicate of [Why does my JavaScript get a "No 'Access-Control-Allow-Origin' header is present on the requested resource" error when Postman does not?](https://stackoverflow.com/questions/20035101/why-does-my-javascript-get-a-no-access-control-allow-origin-header-is-present) – Isma Nov 11 '17 at 12:22
  • Yea but still I didn't get what was given there. – Yash Parekh Nov 11 '17 at 12:27
  • you can try the solution i gave using proxy url i have tested it and it works – Sagar Nov 11 '17 at 12:38

2 Answers2

2

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);
});
Sagar
  • 475
  • 2
  • 8
0

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);
    }
});
Argus Malware
  • 773
  • 7
  • 19
  • jsonp throws error BTC_BCC?callback=jQuery16206304910685867071_1380876031038&_=1510404958275:1 Uncaught SyntaxError: Unexpected token : – Sagar Nov 11 '17 at 12:56