使用 headers 获取

Fetch with headers

当使用 headers return

在服务器中对请求使用 fetch 时
SyntaxError: Unexpected end of input
    at index.js:50
    at <anonymous>

第 50 行代码是 }).then(res => res.json())

有什么问题吗?

此代码提取。

fetch(api-url, {
          mode: 'no-cors',
          method: "POST",
          headers: {
              'Accept': 'application/json',
                        'Content-Type': ' application/json',
                        'X-API-SERVER': '85499f9f'
                    },
        }).then(res => res.json())
          .then(res => {
            if (res.status === 200){
              console.log("accepted");
            }else {
              console.log(res.error);
            }

             console.log(res.error)
          }).catch(err => console.log(err))

由于您请求 API,因此您不想禁用 CORS。只要您的 API 服务器也发送正确的 headers (Access-Control-Allow-Origin),只需在您的 fetch 请求中删除 mode: 'no-cors' 即可解决此问题.