Reading in JSON file generating error

Asked

Viewed 210 times

0

I’m trying to make a read on JSON, and it turns out what I wish on a div but my console is returned the following error,

XMLHttpRequest cannot load

JSON file:

{"content":{"nome":"Josimara","pais":"brasil"}}

HTML

<script type="text/javascript">

    $(function() {

        $.get("arquivo/json", {  },
        function(data){
        $("#nome").html(data.nome);
        }, "json");

    });

</script>

<div id="nome"></div>

What could be this mistake?

  • So this error is because the URL has no active CORS, that is, it blocks external requests. You can do this ajax on the server?

  • @Sergio can not do on the server is an external API that I get the information, what would be the best solution for this case?

  • This API supports JSONP?

  • @Josimara, could you tell us what the API? the point is, she’s probably blocking access by not having the CORS active (as Rgio spoke), and the JSONP would be an alternative to "bypass" the problem

  • @Josimara, if you could pass the link, it would help

  • @Rafaelaugusto Can yes of course, https://help.trustedcompany.com/hc/pt-br/articles/115000861763-API-Dados-de-classifica%C3%A7%C3%A3o-da-loja and https://help.trustedcompany.com/hc/pt-br/articles/213341086-Bestseu-CTR-em-resultados-org%C3%A2nicos-using-Rich-Snippets

  • @Josimara Your Keys are right? This may be influencing problem with CORS

  • Yes @Rafaelaugusto correct, because if it doesn’t generate json

  • @Rafaelaugusto the API company is doing a check on their server to see if CORS is enabled, let’s wait :)

  • @Josimara When they do, tell us how it went ;)

Show 5 more comments

1 answer

1

The Problem is CORS, when testing the code below, I received the following error. Failed to load https://trustedcompany.com/api/v1/company/bbbaterias.com.br?key=f157aee72a787417c20a71ac21708ee8-1b06b0bab68ccc183f78742e3bb8f852: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://127.0.0.1:8081' is therefore not allowed access.

$.ajax({
	type: 'GET',
	url: 'https://trustedcompany.com/api/v1/company/bbbaterias.com.br?key=f157aee72a787417c20a71ac21708ee8-1b06b0bab68ccc183f78742e3bb8f852',
	dataType: 'json',
	cache: false,
	success: function(data){
           $('#app').html(data)
	   console.log(data)
	}
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="app"></div>

Which means you don’t have "permission" to access the server, I ran the test with jsonp and it seems that also did not work, if case to documentação not having something on it, you won’t be able to.

Browser other questions tagged

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