responseData给出错误ReactNative

时间:2017-12-07 06:53:50

标签: rest api react-native

import React from 'react';

class App extends React.Component {

    constructor(props) {
        super(props);
        this.state = {
            isOnline: false // set initial state to false 
        };
    }

    componentDidMount() {
        isOnline().then(online => {
            this.setState({
                isOnline: true; // call setState to update state and rerender App
            });
        });
    }

    render() { // use inline expression to conditionally show <Test/> if isOnline is true

        return (

            <div className="ui container">

              { this.state.isOnline && <Test /> }

            </div>
        );
    }
}

enter image description here

任何想法如何解决?提前谢谢。

2 个答案:

答案 0 :(得分:0)

也许响应数据不是json格式试试这个

fetch(loginApi, {
  method: "POST",
  headers: {
    Accept: "application/json",
    "Content-Type": "application/json"
  },
  body: JSON.stringify({
    Password: password,
    UserName: username
  })
}).then((e)=>{
 console.log(e) //see what you get, if promise then use another then 
})

答案 1 :(得分:0)

幸运的是我找到了解决方案。

var loginParams = {
        Password: password,
        UserName: username
    };

    var loginFormData = new FormData();

    for (var param in loginParams) {
        loginFormData.append(param, loginParams[param]);
    }

    fetch(loginApi, {
     method: "POST",
      headers: { /// <---- you can remove this headers if you are not using any headers in postman..
    Accept: "application/json",
    "Content-Type": "application/json"
  },
  body: loginFormData
})
  .then(response => response.json())
  .then(responseData => {
    console.log(responseData); //// <--- getting error at this line
  })
  .catch(error => {
    console.error(error);
  });

有关详细信息,请查看此enter link description here