Freight calculation with WS post via javascript

3

I'm trying to make the freight calculation of the post via javascript, but when I do the test it does not pull the data and it returns the error to me

  

XMLHttpRequest can not load link ... & nVlDiametro = 0 & sCdMaoPropria = s & nVlValueDeclarado = 200 & sCdAvisoReceiving = s. Response to preflight request does not pass access control check: In 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.

Below is the function in js that 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 the dataType to "jsonp" it returns the xml but I do not know how I could get this information:

  

Uncaught SyntaxError: Unexpected token

    
asked by anonymous 28.07.2017 / 20:00

1 answer

-1

Instead of success , use jsonpCallback . And instead of passing a function, enter its name.

So:

$.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);
}
    
28.07.2017 / 21:40