我一直在尝试使用Postman API发出发布请求,但是我一直收到CORS政策错误。我可以向API发出GET请求,但我一直在努力进行POST请求
我如何拨打电话
var myHeaders = new Headers();
myHeaders.append("X-Api-Key", "The relevant api key");
myHeaders.append("Content-Type", "application/json");
var raw = {
collection: {
variables: [],
info: {
name: "Sample Collection",
description: "This is just a sample collection.",
schema: "https://schema.getpostman.com/json/collection/v2.0.0/collection.json"
},
item: [
{
name: "This is a folder",
description: "",
item: [
{
name: "Sample POST Request",
request: {
url: "echo.getpostman.com/post",
method: "POST",
header: [
{
key: "Content-Type",
value: "application/json",
description: ""
}
],
body: {
mode: "raw",
raw: "{\n\t\"data\": \"123\"\n}"
},
description: "This is a sample POST Request"
},
response: []
}
]
},
{
name: "Sample GET Request",
request: {
url: "echo.getpostman.com/get",
method: "GET",
header: [],
body: {
mode: "formdata",
formdata: []
},
description: "This is a sample GET Request"
},
response: []
}
]
}
}
var requestOptions = {
method: 'POST',
mode: 'cors',
headers: myHeaders,
body: raw,
redirect: 'follow'
};
fetch("https://api.getpostman.com/collections", requestOptions)
.then(response => response.text())
.then(result => console.log(result))
.catch(error => console.log('error', error));
})
回复
Access to fetch at 'https://api.getpostman.com/collections' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
我能够通过Postman进行呼叫,并且工作正常,只是通过JavaScript进行调用时,我似乎收到了此错误,而且我不确定为什么。
任何帮助都会很棒