这是我的代码:

componentWillMount() {
  fetch("http://localmachine/localservice/webservice/rest/server.php", {
    method: 'POST',
    body: JSON.stringify({
      wstoken: 'any_token',
      wsfunction: 'any_function',
      moodlewsrestformat: 'json',
      username: 'user',
      password: 'pass',
    })
  })
  .then((response) => response.text())
  .then((responseText) => {
    alert(responseText);
  })
  .catch((error) => {
    console.error(error);
  });
}

在浏览器中,此请求返回一个令牌,但在我的react-native android应用程序中,返回一个xml错误.

推荐答案

这对我来说很有用

fetch("http://10.4.5.114/localservice/webservice/rest/server.php", {
  method: 'POST',
  headers: new Headers({
             'Content-Type': 'application/x-www-form-urlencoded', // <-- Specifying the Content-Type
    }),
  body: "param1=value1&param2=value2" // <-- Post parameters
})
.then((response) => response.text())
.then((responseText) => {
  alert(responseText);
})
.catch((error) => {
    console.error(error);
});

React-native相关问答推荐

React Native 组件中两次使用map的问题

如何在react-native 中设置视图的自动高度

你使用 react native 应该学习 react.js 吗?

在 React Native 上为 android 创建自定义 UI 组件,如何向 JS 发送数据?

react-native StyleSheet 属性和选项列表

如何从 Firebase 获取所有设备令牌?

请求无法完成,因为您已超出配额

如何在 React Native 中创建和保存 CSV 文件?

TabNavigator 中的 React Navigation 传递props

给定 APK 文件,我如何检测应用程序是否使用 React Native?

元素中缺少main.jsbundle、Foundation和Security,导致报错

当 textInput 聚焦时,第一次touch flatList 不起作用,但是第二次起作用

如何使用功能组件向 ref 公开功能?

获取没有地理位置 API React-native 的国家/城市?

props验证中缺少 React Navigation 'navigation'

React Native Image 不显示,我该如何解决?

React Native如何将this.setState更改传递给父级

如何在react-native中关闭模态

React Native <>(空)组件到底是什么

如何在react-native中验证 TextInput 值?