Webservice调用给出错误。反应性地

时间:2019-03-27 13:55:28

标签: react-native

我这样调用网络服务:

fetch('url', {
            method: 'POST',
            headers: new Headers({
                          Accept: 'application/json',
                        'Content-Type': 'application/json', // <-- Specifying the Content-Type
                }),
            })
            .then((response) =>  response.text())
            .then(leaders => {
              console.log("leader = ", leaders);
}

但是这给了我这样的错误:

Server was unable to process request. ---&gt; Data at the root level is invalid. Line 1, position 1.

1 个答案:

答案 0 :(得分:0)

尝试以下代码:

  fetch('url', {
                method: 'POST',
      headers: {
        Accept: 'application/json',
        'Content-Type': 'application/json',
      },
                body: JSON.stringify(collection) // <-- Post parameters
                })
                .then((response) =>  response.text())
                .then(leaders => {
                  console.log("leader = ", leaders);
    }

或者您可以直接致电:

   fetch(url)
      .then(response => response.json())
      .then(data => this.setState({ data }));