Error: No Access-Control-Allow-Origin

Asked

Viewed 39 times

0

[HELP] I am not being able to access my server’s JSON; I’m using this code to access, when I use the urlSrt, it returns me normally, (it is a site that receives a cep and returns a json with the information); But when I try it on my not working api.

$(document).ready(() => {
console.log("Página carregada");
$("#descricao").focusout(() =>{
const cep = $("#descricao").val();

const urlStr = "https://viacep.com.br/ws/" + cep + "/json/";
const urlStr2 = "ip" + ":3000/categorias/1";

$.ajax({
url: urlStr2,
type: "GET",
dataType: "json",
crossDomain : true,
success: (data) => {
console.log(data);
},
error: ((error) => {
console.log(error);
})
})
});
})´´´

I checked some tutorials that said to release the permission , but it still didn’t work.;

app.use(function(req, res, next) {
res.header('Access-Control-Allow-Origin', "*");
res.header('Access-Control-Allow-Methods','GET,PUT,POST,DELETE');
res.header('Access-Control-Allow-Headers', 'Content-Type');
next();
})´´´

Finally, I’m out of ideas

  • 1

    Are you trying to access through a front end application? If you have access to the back end server, perform Cors edits on the back if you do not have a proxy configuration on your front

  • Exactly, I even have access to the server, and in it I put the res.header('Access-Control-Allow-Origin', "*"); but still it continues with the error. How would you do this proxy setting?

  • Tried to use dataType: "jsonp" or $.support.cors = true; ?

No answers

Browser other questions tagged

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