首页 > 解决方案 > 在 react-native 中将数据提取到数据库时出现问题。错误:“json parse error unrecognized token '<'”

问题描述

try {
         return fetch('http://mylinktoAPI', {
              method: 'POST',
              headers: {
                  'Accept': 'application/json',
                  'Content-Type': 'application/x-www-form-urlencoded',
              },
              body: JSON.stringify({
                  first_name: this.state.first_name,

                  last_name: this.state.last_name,

                  gender: this.state.gender,

                  blood_group: this.state.blood_group,

                  phone: this.phone,

                  email: this.email,

                  address: this.address,

              }),

          }).then((response) => response.json())
              .then((responseJson) => {
              Alert.alert(responseJson);
          }).catch((error) => {
              console.error(error);
          });
      } catch (e) {
          console.error('fetch catch error', e);
      }
};

标签: apireact-nativefetch

解决方案


在标题中,您可以尝试将 Content-Type 更改为"application/json"

"Content-Type", "application/json"

推荐阅读