help I have an error when consuming api from ajax

0

the browser throws me this error when trying to consume API, I do not know what it can be:

  

Failed to load    link : No   'Access-Control-Allow-Origin' header is present on the requested   resource. Origin 'null' is therefore not allowed access.

my code:

$.ajax({
 url: 'https://bitscoinmarketcap.com/ws/v1/ticker/?coinName=bitcoin',
 type: "get",
 dataType: 'json',
})
.done(function(recursos) {
 console.log(recursos)
})
.fail(function() {
console.log("error api");
});
    
asked by Juan David Delgado J 09.07.2018 в 23:37
source

1 answer

0

Econtre this solution I do not know how optimal it is, but it worked to the error that came out, place this code in my function in php and when making a request from ajax I boto the information

if (isset($_SERVER['HTTP_ORIGIN'])) {  
    header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");  
    header('Access-Control-Allow-Credentials: true');  
    header('Access-Control-Max-Age: 86400');   
}  

if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {  

    if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']))  
        header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS");  

    if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']))  
        header("Access-Control-Allow-Headers: {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");  
}  
    
answered by 10.07.2018 в 00:28