ReactJS - 获取函数:为什么我没有被捕获(在promise中)SyntaxError:JSON输入的意外结束?

时间:2016-08-10 17:41:57

标签: javascript json reactjs fetch react-jsx

GET请求成功运行,我现在正尝试使用以下内容对网址进行POST:

var data = {
  'Content-Type': 'application/json',
  'number': 'TEST12345'
}

var post = {
    "number": "TEST12345",
    "parameters":
    {
        "START_NUMBER" : {
          "value": 5,
          "effectiveDate": "2016/01/01 10:10"
        }
    }
}

var myInit = {
  method: 'POST',
  mode: 'cors',
  headers: data,
  body: JSON.stringify(post)
};

fetch('http://localhost:8080/form/upload/post/save', myInit)
.then(result=>{console.log(result.json())})

但是我得到了以下日志:

enter image description here

所有似乎都是正确的,但为什么错误:Uncaught (in promise) SyntaxError: Unexpected end of JSON input

1 个答案:

答案 0 :(得分:1)

body方法返回一个promise

fetch('http://localhost:8080/form/upload/post/save', myInit)
  .then(result => result.text())
  .then(data => console.log(data))

https://developer.mozilla.org/en-US/docs/Web/API/Body/text