Problems using callback on a Ws

Asked

Viewed 256 times

0

I am implementing integration with a WS which searches the data on the basis of cnpj.

When consulting the WS and request a callback, it returns me the following ERROR:

Uncaught Syntaxerror: Unexpected token :

I believe you’re misinterpreting Json.

The code I’m using is in this remap:

<html>
<head>
<title>CNPJ Webservice</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<!-- Adicionando Javascript -->
<script type="text/javascript" >


function meu_callback(conteudo) {

    //Atualiza os campos com os valores.
    document.getElementById('txtRazao').value=(conteudo.nome);
}

function pesquisacep(valor) {

    //Nova variavel "cep" somente com d�gitos.
    var cnpj = valor;

    //Cria um elemento javascript.
    var script = document.createElement('script');

    //Sincroniza com o callback.
    script.src = '//receitaws.com.br/v1/cnpj/'+ cnpj + '/?callback=meu_callback';

    //Insere script no documento e carrega o conte�do.
   document.body.appendChild(script);

};

</script>
</head>

<body>
<!-- Inicio do formulario -->
  <form method="get" action=".">
    <label>Cep:
    <input name="txtCNPJ" type="text" id="txtCNPJ" value="" 
           onblur="pesquisacep(this.value);" /></label><br />
    <label>Nome:
    <input name="txtRazao" type="text" id="txtRazao" size="60" /></label><br />
  </form>
</body>

</html>

1 answer

-1

The main problem is that the url api wing did not allow CORS. You cannot be accessed via ajax. Another problem you have is that you cannot use accents when declaring functions. eg clean_formulário_cnpj(). Should be clean_formulario_cnpj ().

Proof this in php:

if ($_GET['cnpj']) {
    $url = 'http://receitaws.com.br/v1/cnpj/' . $_GET['cnpj'];
    $dadosSite = file_get_contents($url);
    header('Content-Type: application/json');
    echo $dadosSite;
}

Browser other questions tagged

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