Deezer API - JSON request error with $.getJSON

Asked

Viewed 148 times

3

When trying to perform a simple search on Deezer API (Public Search API without Need for Authentication Key) I get the following error message as return:

XMLHttpRequest cannot load http://api.deezer.com/search?q=Bang. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access.

The Code used to perform the search:

$.getJSON(
    "http://api.deezer.com/search?q=Bang",
    function(dados) {
        console.log(dados);
    }
);

1 answer

2

To make calls between domains it is necessary to use JSONP, and for the server to know that it expects a JSONP as return it is necessary to add the parameter output=jsonp.

$.ajax({
    type: 'get',
    url: "http://api.deezer.com/search?q=Bang&output=jsonp",
    dataType: 'jsonp',
    contentType: "application/json; charset=utf-8",
    jsonpCallback: "jsonCallback",
    cache:false,
    beforeSend: function(xhr) {
        // função antes de executar a chamada
    },
    success: function(data,statusText,xhr) {
        // função quando ocorrer sucesso
    }
});

function jsonCallback (data) {
    console.log(data);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.