javascript - ReactJS - 获取函数: Why am I getting uncaught (in promise) SyntaxError: Unexpected end of JSON input?

标签 javascript json fetch reactjs

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

一切似乎都是正确的,但为什么会出现错误:未捕获( promise 中)语法错误:JSON 输入意外结束

最佳答案

主体方法返回一个 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

关于javascript - ReactJS - 获取函数: Why am I getting uncaught (in promise) SyntaxError: Unexpected end of JSON input?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38880155/

相关文章:

react-native - 如何使用 fetch() 和 React Native 处理 set-cookie header

javascript - 使用 window.ethereum 获取 toWei 函数

javascript - Docker 本地 js 文件 IO 错误

javascript - jquery 数据表隐藏列

javascript - 错误 : Expression 'undefined' used with directive is non-assignable

javascript - 如何将动态参数传递给axios实例

javascript - 如何正确自定义第三方React组件

java - 与 Jackson 映射时将默认值设置为空字段

javascript - 从对象内的 JSON 解析对象获取值;动态键 (JavaScript)

javascript - 数据抓取帮助 html 和 javascript