How to consume a Soap (do cadsus) api via javascript(Browser)?

Asked

Viewed 350 times

-2

Today I can consume Cadsus Aces by dotnet c#. However, now I need to consume these same Apis(cadsus) via javascript(screen), but, I’m getting the following error when I make the following request via browser:

Access to XMLHttpRequest at 'https://servicos.saude.gov.br/' from origin 'http://localhost:4566' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
POST https://servicos.saude.gov.br/ net::ERR_FAILED

Somebody’s been through this trouble and they know how to fix it?

my code is as follows:

<script>
function soap() {
        var xmlhttp = new XMLHttpRequest();
        var url = $('#url').val();
        var SOAPEnvelope = $('#SOAPEnvelope').val();
        xmlhttp.open('POST', url, true);
        xmlhttp.onreadystatechange = function () {
            if (xmlhttp.readyState == 4) {
                if (xmlhttp.status == 200) {
                    alert(xmlhttp.responseText);
                }
            }
        }
        xmlhttp.setRequestHeader('SOAPAction', 'https://servicos.saude.gov.br/cadsus/PDQSupplier');
        xmlhttp.setRequestHeader('Content-Type', 'text/xml');
        xmlhttp.send(SOAPEnvelope);
    }
</script>

1 answer

-3

By the error message shown, a header is missing, Access-Control-Allow-Origin adds:

xmlhttp.setRequestHeader('Accept', '*/*');
xmlhttp.setRequestHeader('Access-Control-Allow-Origin', '*');

Browser other questions tagged

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