Question :
When I try to perform a simple search in the Deezer API (Public Search API without Authentication Key) I get the following error message as a 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);
}
);
Answer :
To make cross-domain calls, you need to use JSONP, and for the server to know that you expect a JSONP to return, you must add the output=jsonp
parameter.
$.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>