How to get a .JSON from a URL with JS

0

I looked at some examples of the jQuery documentation, but I still could not.

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 can not load link . In 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.

Uncaught SyntaxError: Unexpected token u

    
asked by anonymous 28.10.2015 / 18:29

2 answers

-1

You need to configure your API to accept Cross Domain requests

This article explains how to do this if you use php: link

And if you use C #: link

If you use Java or Python you have a link within the first post.

    
28.10.2015 / 19:20
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]);
    
28.10.2015 / 18:37