在Android中,React-Native Fetch“ POST”请求抛出“ SyntaxError:JSON输入意外结束”

Haf*_*eem 3 json http-post react-native

这是我的功能
不知道问题出在哪里

fetchData() {
        fetch(REQUEST_URL, {
      method: 'POST',
      headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json',
      },
      body: JSON.stringify({
        Request : 'menu',
        active : '1',
      })
    }).then((response) => response.json())
    .then((responseData) => {
        this.setState({
              menu: responseData.Response,
          }); 
    })
    .catch((error) => {
      console.warn('error',error);
    })
    .done();
      }
Run Code Online (Sandbox Code Playgroud)

请指出功能上的问题

Gab*_*upu 5

发生该错误是因为您的响应无法转换为JSON格式。可能由于错误的标头,响应正文或基于服务器的其他各种原因而发生此问题。由于显然响应不一致,因此,方法是在尝试将响应转换为JSON之前执行服务器响应的其他验证。

您可以通过以下方式来实现:

.then((response) => response.json())
Run Code Online (Sandbox Code Playgroud)

.then((response) => response.json())
Run Code Online (Sandbox Code Playgroud)