HTTP GET - Jquery - Node.js request

Asked

Viewed 333 times

-1

First of all, I wanted to say that I’m developer . net, so I’m kind of lost in what I’m doing.

If I understood correctly, I downloaded Node.js and inside the Node.js prompt I gave a CD in the folder of my project (where is my index.html and index.js), after the cd gave an npm start, then it went up as localhost:9966 and I am using the page since then, all apparently working well well.

But now I’m locked into a GET/POST request with AJAX, and the weirdest thing, when I do a GET for the VIACEP api I get the return normally, but when I send it to my api. net he gives me error of CORS.

Follows the Code

function getCep(){

    $.ajax({
        url: 'https://webchatapi.azurewebsites.net/api/atendimento/solicitacoes/',
        //url: 'https://viacep.com.br/ws/09831380/json/ ',   
        headers: {'content-type': 'application/json'},
        crossDomain: true,
        type: "GET",
        dataType: "json",
        success: function (data) {
            console.log(data);    
        },
        error: function () {
            console.log("error");
        }
    });
}

When I use the VIACEP url:

Viacep

When I use my . net api

Api .net

result of my api when get by Chrome/Postman:

Chrome/Postman

I’ve been stuck in this for two days, I don’t know what to do, thank you !

  • Good afternoon Icaro, I had already visited this link before asking the question, as you can see in my code already includes "crossDomain: true" and would not like to change my application to extension, also already tried to use jQuery.support.Cors = true, however do not know how to enable CORS in Node.js as informed "res.setHeader('Access-Control-Allow-Origin', '*'); ", where I put this code?

  • I put the link only as related. = D -- On your question it seems to me that your site localhost this one with res.setHeader..., but this error is being caused by the site webchatapi.azurewebsites.net it is he who is refusing the requisitions of other domains.

  • I figured it was something with the API, but in it I’ve already enabled the Cors :(

1 answer

0

I managed to solve the problem.

Apparently my Node.js server and were normal, the problem was in my . net API which was not enabled Cors!

I followed this documentation and now everything works normally:

Documentation . net CORS

Browser other questions tagged

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