Request error - GET method, Javascript, Ajax,

Asked

Viewed 32 times

0

Hello,

I’m trying to make a request but it’s not bringing the data

script js.


        $.ajax({
            url : "https://dwconsorcios.com.br/api/v1/contemplados",
            type : "GET",
            dataType:"json",
            success:function (data) {
                console.log(data);
                $("#codImovel").val(data.id);
            }, 
            error:function(erro){
                console.log(erro);
            } 
            });
});

index.html

<body>
<script src="https://code.jquery.com/jquery-1.9.1.js"></script>
<script language="JavaScript" type="text/javascript" src="public_contemplados/script.js"></script>
<output id="codImovel"></output>
<output name="codImovel"></output>
</body>
  • GET, or POST? in question speaks post but in code is get

  • 2

    is GET, adjusted the question

  • Ran on the server?

  • yes was given run

  • and what returns? any error? already looked in the nvaegador tools if there is any message?

  • shows this error 'Access to Xmlhttprequest at 'https://dwconsorcios.com.br/api/v1/contemplados' from origin 'https://ymove.com.br' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested Resource.'

Show 1 more comment

1 answer

0

@Andre Miranda - Xservice, by the result shown, you are trying to get the information of the domain dwconsorcios.com.br, using the domain ymove.com.br.

In this case the access of data via Ajax is blocked by the browser when trying to access the request of another domain, even if the request is made by GET method.

If you are one of the representatives of this domain dwconsorcios.com.br, you should put this configuration below on the application server (IIS, Apache, Nginx), adding in the header information (header) of the API response, or in the application itself:

name = "Access-Control-Allow-Origin" value = "*"

If you still have questions, see this link below:

  • Another possibility would be to access the requested address directly from the browser and can save the results in a json file and put directly on your server. But I believe this is not your idea, because maybe you are trying to fetch the data dynamically, at least this is the url, it is an API. Well, this is still a possibility.

Browser other questions tagged

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