Problems using callback in a ws

0

I'm implementing integration with a WS that fetches data based on cnpj .

When querying WS and requesting a callback , it returns me the following ERROR:

  

Uncaught SyntaxError: Unexpected token:

I think you're having a misinterpretation of JSon .

The code I'm using is in this repo :

<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>

    
asked by anonymous 24.03.2016 / 15:15

1 answer

-1

The main problem is that the api url wing did not allow CORS. You can not access it via ajax. Another problem you have is that you can not use accents when declaring functions. for example cleans_format_cnpj (). It should be clean_formula_cnpj ().

Try 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;
}
    
03.01.2017 / 20:51