0
votes

i want to call api to my website :

jQuery(document).ready(function($) {
    if ( $('#main_').length ) {
    $.getJSON("http://url", function(data) {
        var branches = '';
        var bibs = '';
        var items = '';
        $.each(data, function(key, value){branches += '<div>'+value.homebranch+'</div>'});
        $.each(data, function(key, value){bibs += '<div>'+value.bibs+'</div>'});
        $.each(data, function(key, value){items += '<div>'+value.items+'</div>'});
        $('div.newsitem').prepend('<div class="newsitem" id="mystats"><table class="table table-striped" style="width: 100%; background: none;"><thead><th colspan="3" style="text-align: center; font-weight: bold; padding: 8px; line-height: 1.42857143; vertical-align: middle; text-transform: uppercase;">Library Statistics</thead><tbody><tr><td><strong>Branch</strong></td><td><strong>Unique titles</strong></td><td><strong>Total Copies</strong></td></tr><tr><td class="text-center">'+branches+'</td><td class="text-center">'+bibs+'</td><td class="text-center">'+items+'</td></tr></tbody></table></div>');
    });
    }
});

Access to XMLHttpRequest at 'http://url' from origin 'http://website' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

2

2 Answers

0
votes

If I understood it right you are doing an XMLHttpRequest to a different domain than your page is on. So the browser is blocking it as it usually allows a request in the same origin for security reasons.

You have to add the 'Access-Control-Allow-Origin' header to the response

0
votes

Where you set response headers you can add: Access-Control-Allow-Origin: * to allow every domain to access your api