How to receive a JSON object from an external url / domain?

6

I found a lottery API that brings me the result data of a lottery of Lotofácil, but I'm not able to access the data of the JSON object.

I'm trying this way:

$(document).ready(function(){
    $.get( "http://developers.agenciaideias.com.br/loterias/lotofacil/json", function( data ) {
      $( ".result" ).html( data );
      alert( "Load was performed." );
    });
}); 

But the browser console shows me this error:

  

XMLHttpRequest can not load    link . At the   'Access-Control-Allow-Origin' header is present on the requested   resource. Origin ' link ' is therefore not allowed   access.

When I access the URL directly I down the JSON file normally, so I would like to know what this error is about and how to make the right call to receive that data.

    
asked by anonymous 24.10.2014 / 19:25

5 answers

3

Thanks to Caffé's response, through PHP I was able to get the object and its values, like this:

$json = file_get_contents('http://developers.agenciaideias.com.br/loterias/lotofacil/json');
$jsonDecode = (json_decode($json, true));
    
24.10.2014 / 20:39
5

This answer says JavaScript code is limited by security policy same-origin policy .

That is, what you're trying to do (get content from another domain via JavaScript) is blocked by the browser.

Also, according to the same answer, in order for this type of access to be possible, you would need a configuration in the target domain (developers.agenciaideias.com.br).

That is, you will not be able to get this content from JavaScript by running it in the browser. You will have to get on the server side of your application and give your user a ready page with the content.

    
24.10.2014 / 20:05
5

You can use Yahoo Query Language to dribble some Same- origin Policy .

Example:

$.getJSON("http://query.yahooapis.com/v1/public/yql",
{
    q: 'select * from json where url="http://developers.agenciaideias.com.br/loterias/lotofacil/json"',
    format: "json"
},
function(data){
    if (data.query.results) {
        console.log('data',data.query.results.json)
    } else {
        console.error('no results')
    }
});

Prints:

{
  "concurso": {
    "numero": "1122",
    "data": "ES",
    "cidade": "<span style=\"font-size: 12px; font-weight: bold;\">Estimativa de Prêmio</span><br /><span style=\"color: rgb(102, 102, 102); font-size: 22px; font-weight:bold;\">R$</span> <span style=\"font-size: 22px; color:#911687; font-weight: bold;\">1.700.000,00</span><br /><span style=\"font-size: 11px;\">*para o próximo concurso, a ser realizado em 24/10/2014</span><br /><br />-<a href=\"javascript:imprimir_lotofacil(1122);\"><img src=\"/loterias/_images/button/btn_imprimir_resultadojogo.jpg\" width=\"148\" height=\"73\" border=\"0\" /></a>",
    "local": "Caminhão da Sorte",
    "numeros_sorteados": [
      "02",
      "03",
      "07",
      "10",
      "11",
      "12",
      "13",
      "14",
      "16",
      "19",
      "20",
      "21",
      "23",
      "24",
      "25"
    ],
    "premiacao": {
      "acertos_15": {
        "ganhadores": "4",
        "valor_pago": "428.378,51"
      },
      "acertos_14": {
        "ganhadores": "507",
        "valor_pago": "1.485,58"
      },
      "acertos_13": {
        "ganhadores": "19.267",
        "valor_pago": "15,00"
      },
      "acertos_12": {
        "ganhadores": "242.287",
        "valor_pago": "6,00"
      },
      "acertos_11": {
        "ganhadores": "1.252.543",
        "valor_pago": "3,00"
      }
    }
  },
  "proximo_concurso": {
    "data": "23",
    "valor_estimado": "20"
  }
} 

Reference : How to scrape content from other sites using jQuery?

    
24.10.2014 / 21:17
2

The browser protection system does not allow the exchange of xml or json from different domains, however, it allows the use of "link" calls to javascript scripts from external URLs. Using jQuery with dataType of type "jsonp" (with P); This format informs jQuery to use a "trick" that "tricks" the browser ;-), internally the request is made as a script link. I have used it for a long time and it WORKS!

{
jQuery.ajax({
        url  : 'http://www.respondePaginasWeb',
        data : 'cliente=eu&acao=getmenu',
        type : "GET",
        crossDomain  : "true",
        dataType     : "jsonp",
        contentType  : "application/json",
        success: function( menu ){
            console.log(menu);
        }
    });
}
    
19.06.2015 / 23:14
1

This url is blocked here at work, try something like this to see what it does:

$.ajax({ 
  url:"http://developers.agenciaideias.com.br/loterias/lotofacil/json", 
  type:"GET", 
  crossDomain: "true", 
  dataType: 'application/json', 
  success: function (d) { 
     console.log(d) 
  }
})
    
24.10.2014 / 19:36