weixin_33744141 2017-11-11 12:21 采纳率: 0%
浏览 40

JSON API获取请求错误

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.

</div>
  • 写回答

2条回答 默认 最新

  • weixin_33682719 2017-11-11 12:37
    关注

    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);
    });
    
    评论

报告相同问题?