How to catch a . JSON from a JS URL

Asked

Viewed 3,379 times

0

I looked at some examples of jQuery documentation, but I haven’t been able to.

Code:

<!DOCTYPE html>
<html lang="pt-BR">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <script src="http://code.jquery.com/jquery-2.0.3.min.js" type="text/javascript"></script> 
    <script type="text/javascript">
        var endereco = 'http://10.20.229.17:8085/todosPostos';

        $.ajax({
            url: endereco,
            complete: function(res){
                var data = JSON.parse(res.responseText);
                console.log(data); 
            }
        });  
    </script>
</head>
<form action="testeste.html" method="post">
    <select>
        <option value="trechoorigem">Trecho (Origem)</option>
        <option value="trechodestino">Trecho (Destino)</option>
    </select>
    <input type="text" />
    <button type="button">Pesquisar</button>
</form>
</body>



</html>

Console:

Xmlhttprequest cannot load http://10.20.229.17:8085/todosPostos. No 'Access-Control-Allow-Origin' header is present on the requested Resource. Origin 'null' is therefore not allowed access.

Uncaught Syntaxerror: Unexpected token u

  • 1

    You are performing a cross Domain call, that is, trying to consume the API (http://10.20.229.17:8085/) from another server (or domain). You need to enable your API to allow Cross-Omain requests.

  • This answer right here in the OS explains a little about CORS: http://answall.com/questions/3183/requisi%C3%A7%C3%A3o-ajax-cross-Domain-with-javascript-pure-without-Apis#Answer-3251

  • @Pedrocamarajunior I don’t know if this influences anything, but this API is on a VPN that I’m connected to. Another problem is that I’m not getting JSON either locally.

  • If for example, you are calling on your page (localhost:8080) and your api is at another address (ex localhost:9999) you are already calling cross Omain. I recommend changing the API to accept this type of request. The configuration will depend on the rpogram language you are using in the API.

  • 2

    inside the ajax, place: crossDomain: true,, as per this example: http://answall.com/questions/54524/comor-show os-values-storage-no-json-javascript/95061#95061 and in PHP: before json_encode(), put this: header("Access-Control-Allow-Origin: *");

  • @Ivanferrer $.ajax({&#xA; type: "POST",&#xA; dataType: "json",&#xA; crossDomain: true,&#xA; url: 'http://10.90.226.87:8085/todosPostos',&#xA; complete: function(res){&#xA; var data = JSON.parse(res.responseText);&#xA; console.log(data); &#xA; }&#xA; }); the date returns: Xmlhttprequest cannot load http://10.90.226.87:8085/allPosts. No 'Access-Control-Allow-Origin' header is present on the requested Resource. Origin 'null' is therefore not allowed access.

  • I do not know if there is this "complete", I think is "Success". and other, in ajax, do not need to parse, read in the answer of the link I gave you.

Show 2 more comments

2 answers

0

You can use the native method:

To pass:

var json_text = JSON.stringify(your_object, null, 2);
var url + '?' + json_text;

To recover:

var json = JSON.parse(url.split('?')[1]);
  • Emir, I don’t get it right. Can you apply this method in the sample code I posted in the question?

  • I don’t understand your code. Are you trying to convert the return of your query right? From a console.log on res. Apparently a valid json is not being returned.

  • Yes, until now I was developing with json in a variable, but now I need to take a URL. There’s no way you can see there, because this API is on a VPN where I have access.

  • Post through the console.log the contents of the res variable

  • http://i.imgur.com/Ukpxorw.jpg Here’s the res.log console

  • 1

    Apparently your problem is not in the call. Are you passing the data correctly? The server expects a post or get etc?

  • I can’t answer that Emir, about the server I don’t know what goes on there.

  • The server belongs to a third party?

Show 3 more comments

-1


  • It would be interesting if you post an excerpt of the article related to this in case the site ever goes off the air or change the address not give display problems :)

  • I get it, but I’ll explain my situation to see if you can help me out. I am using this API to develop a Dashboard in HTML+JS, I will assemble some tables and graphs with the extracted data. But this Dashboard will run on the same API server. I wanted to use only JS to get this data and popular my charts/tables, but I’m not even able to get the data from a file. JSON local.

  • 1

    Doing what I said you will be able to solve the problem of the example you posted, as for the case of local JSON you should post more details because it is very vague. I have no way of knowing what error occurs, how you are making this call, can be many things. I think this is content for another question.

Browser other questions tagged

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