Freight calculation with WS post office via javascript

Asked

Viewed 2,416 times

3

I’m trying to calculate the mail freight via javascript, but when I do the test does not pull the data and returns me the error

Xmlhttprequest cannot load http://ws.correios.com.br/calculador/CalcPrecoPrazo.asmx/CalcPrecoPrazo?nCd…&nVlDiametro=0&sCdMaoPropria=s&nVlValorDeclarado=200&sCdAvisoRecebimento=s. Response to preflight request doesn’t pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested Resource. Origin 'null' is therefore not allowed access.

Below is the js function I’m using.

function calcWsCorreios{
var sendjson = {   
"nCdEmpresa":"",
"sDsSenha":"",
"nCdServico":"41106",
"sCepOrigem":"37540000",
"sCepDestino":"37540000",
"nVlPeso":"1",
"nCdFormato":"1",
"nVlComprimento":"20",
"nVlAltura":"5",
"nVlLargura":"15",
"nVlDiametro":"0",
"sCdMaoPropria":"s",
"nVlValorDeclarado":"200",
"sCdAvisoRecebimento":"s"
}



$.ajax({
type: "GET",
contentType: "application/json; charset=utf-8",
url:
"http://ws.correios.com.br/calculador/CalcPrecoPrazo.asmx/CalcPrecoPrazo",
data: sendjson,
dataType: "json",
success: function (data) {
        console.log(data);
    },
    error:function (data,err) {

        console.log(data);
        console.log(err);
    },
});

}

If I change dataType to "jsonp" it returns the xml but I don’t know how I could get this information:

Uncaught Syntaxerror: Unexpected token <

imagem com o erro e dados retornados

1 answer

-2

Instead of success, utilize jsonpCallback. And instead of passing a function, enter her name.

Thus:

$.ajax({
    type: "GET",
    contentType: "application/json; charset=utf-8",
    url: "http://ws.correios.com.br/calculador/CalcPrecoPrazo.asmx/CalcPrecoPrazo",
    data: sendjson,
    dataType: "jsonp"
    jsonpCallback: "foo"
}

function foo (resultado) {
    // verifique que resultado é JSON válido
    console.log(resultado);
}
  • So gives the same image error. Because the web service return is in xml.

  • Solved, @Shikaizi? I’m in real trouble

  • Unfortunately not. In the end I didn’t need to use the WS of the post office anymore!

Browser other questions tagged

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