Consume WebService from an external link

1

When consuming a webService from an external URL through the link in the browser link returns the following JSON message:

  

{"add-in": "from 9201 to the end - even side (even side belongs to   the "Count"), "neighborhood": "Barreiro", "city": "Belo Horizonte",   "public place": "Avenida Teresa Cristina", "estado_info": {"area_km2":   "Zip": "31", "name": "Minas Gerais"}, "zip":   "30640240", "city_info": {"area_km2": "331,401", "codigo_ibge":   "3106200"}, "state": "MG"}

How can I recover this data?

I have used the Code:

<asp:TextBox ID="txtCep" runat="server"></asp:TextBox>
    <asp:Button OnClientClick="buscarCep();" ID="btnCep" runat="server" />

    <script type="text/javascript">
        function buscarCep() {
            var cep = $("#<%=txtCep.ClientID%>").val();
            var url = "http://api.postmon.com.br/v1/cep/" + cep;
            $.ajax({
                url: url,
                data: "{}",
                dataType: "json",
                type: "POST",
                contentType: "application/json; charset=utf-8",
                success: function (data) {
                    var dados = JSON.parse(data.d);

                },
                error: function (response) {
                    alert(response.responseText);
                },
                failure: function (response) {
                    alert(response.responseText);
                }
            });
        }
    </script>

More has generated the following 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. The response had HTTP status code 405.

    
asked by anonymous 16.01.2015 / 15:37

2 answers

1

Try this:

  
    function buscarCep() {
    var cep = $("#txtCep").val();
    var url = "http://api.postmon.com.br/v1/cep/" + cep;
    $.getJSON(url,function(data){
        console.log(data);
    });
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script><buttontype="button" onclick="buscarCep()">Buscar Cep</button>
    <input type="text" id="txtCep" />

There are some answers here explaining why.

  

Browsers have something called "same domain policy",   which, in short, means that the browser will only load files   via XMLHttpRequest if the destination is in exactly the same domain as the   (which appears in the browser's address bar).

Response link: link

    
16.01.2015 / 17:34
1

Friend, try adding the jQuery library in your code

<script src="https://code.jquery.com/jquery-2.1.3.min.js"></script>

Thisisbecausethe$.signisajQuerycommand,soitwasgivingerrorinyourcode

Hereitwent!ButIwasnotallowedtoreceivethedata,youmayhavethispermission.

Ifitmakesthesamemistakewithyou!Tryusingmycodebelow,there'snomistake!

$.getJSON("http://republicavirtual.com.br/web_cep.php",{"cep" : cep, "formato" : "json"}, function(result){
            //Se deu certo
            if (result['resultado']){
                $("#uf").val(result["uf"]);
                $("#cidade").val(result["cidade"]);
                $("#bairro").val(result["bairro"]);
                $("#logradouro").val(result['tipo_logradouro'] +" "+ result['logradouro'])
                $("#numero").focus();
            }
        });

I hope I have helped! =)

    
16.01.2015 / 17:10