本文介绍了带标题获取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在服务器中使用标头返回获取请求时,

When use fetch for request in server using headers return

SyntaxError: Unexpected end of input
    at index.js:50
    at <anonymous>

代码50行是}).然后(res => res.json())

Line of code 50 is }).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服务器也发送正确的标头(> Access-Control-Allow-Origin ).

Since you're requesting an API, you don't want to disable CORS. It's probably enough to just remove mode: 'no-cors' in your fetch request to fix this, as long as your API server sends the correct headers as well (Access-Control-Allow-Origin).

这篇关于带标题获取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 03:52