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

时间:2016-04-05 07:07:48

标签: javascript reactjs redux

我尝试将isomorphic-fetchreactjs应用程序一起使用redux-thunk中间件。但是当我做

时,我会继续获得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
            })

即使服务器返回{"user": "happy tree friends"}之类的内容。 我使用这个模块的方式有什么问题,或者我的服务器只返回错误的JSON数据。

帮助!!!

2 个答案:

答案 0 :(得分:3)

也许是您的API,返回empty以便您收到错误

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

答案 1 :(得分:2)

事实证明,我必须配置我的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()

@ nour-sammour谢谢!我回去检查我的服务器,这是问题的来源。