使用Javascript发出发布请求获取CORS政策问题

我一直在尝试使用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进行通话时,我似乎收到了此错误,而且我不确定为什么。

任何帮助都会很棒