`同构的fetch`response.json()方法总是抛出意外的输入结束错误

`isomorphic-fetch` response.json() method always throw Unexpected end of input error

本文关键字:意外 输入 错误 结束 方法 fetch 同构 response json      更新时间:2023-12-20

我尝试将isomorphic-fetch与带有redux-thunk中间件的reactjs应用程序一起使用。但当我做时,我一直得到那些Unexpected end of input error

let config = {
    method: 'POST',
    headers: { 'Content-Type':'application/x-www-form-urlencoded' },
    mode: 'no-cors',
    body: "username=foo&password=bar"
};
fetch('my_url', config)
        .then(response => {
                response.json().then(user => ({user, response})) // here is where the error always been thrown
            })

即使服务器返回类似CCD_ 5的内容。我使用这个模块的方式有什么问题吗?或者我的服务器只是返回了错误的JSON数据。

救命!!!

也许您的API返回empty,这样您就会得到错误

JSON.parse('')将抛出Uncaught SyntaxError: Unexpected end of input

事实证明,我必须配置我的tornado服务器才能使用前端部分。

我正在向服务器发出CORS请求,但在服务器默认标头中添加了一些标头。请求成功了:

class BaseHandler(tornado.web.RequestHandler):
  def set_default_headers(self):
      self.set_header("Access-Control-Allow-Origin", "*")
      self.set_header("Access-Control-Allow-Headers", "x-requested-with, content-type")
      self.set_header('Access-Control-Allow-Methods', 'POST, GET, OPTIONS')
  def options(self):
      # no body
      self.set_status(204)
      self.finish()

@努尔,谢谢你!我回去查看了我的服务器,发现问题就出在那里。