problem description
request a third-party interface, and use node, express and http-proxy-middleware to build a server to forward the request.
the front end sends the request using fetch. Some interfaces are accessible, while others are still prompted to cross-domain.
Q:
Why is this the case? if you request the server to forward the data directly in the browser, you can get the data normally. But sending a request with fetch prompts you to cross-domain.
some configurations of fetch:
const getFetch =(url)=> {
try {
let result = fetch(url, {
// credentials: "omit",
// "Content-Type": "application/json;charset=utf-8",
// headers: {
// "Access-Control-Allow-Origin": "*",
// Accept: "application/json, text/plain, */*"
// "Content-Type": "application/json;charset=utf-8"
// },
// mode: "cors"
})
return result.then(res=>res.json());
}catch(err){
console.error(err)
}
}
const postFetch =(url,data)=> {
let obj2params = (obj) => {
let item, result = ""
for ( item in obj ) {
result += "&" + item + "=" + encodeURIComponent(obj[item]);
}
if (result) {
result = result.slice(1);
}
return result;
}
try {
let result = fetch(url, {
method: "POST",
credentials: "omit",
headers: {
"Accept": "application/json, text/plain, */*",
"Content-Type": "application/json"
},
// body: obj2params(data)
body: JSON.stringify(data)
});
return result.then(res=>res.json());
}catch(err) {
console.error(err);
}
}