【问题标题】:'fetch' call comes back as undefined?'fetch' 调用返回为未定义?
【发布时间】:2018-09-14 06:33:55
【问题描述】:

我目前正在开发一个从 php 后端检索数据的 react-native 应用程序,当我调用以下函数时,我的编译器会不断返回 "'fetch' 不是 def" 有谁知道我为什么会出错?

onButtonPress = () => {  fetch('http://www.fitnessinspirationtechnologies.com/ReactNative/login_RN.php', {
method: 'POST',
headers: {
  Accept: 'application/json',
  'Content-Type': 'application/json',
},
  body: JSON.stringify({
    response: 'response',
    id: 'id',
    type: 'type'
  })
})
.then((response) => response.json())
.then((res) => {
    console.log(res.response, res.id, res.type);
})
.done();

}

【问题讨论】:

标签: php reactjs react-native post fetch


【解决方案1】:

Fetch 仅在前端,在某些浏览器中尚不支持。我猜你的反应原生环境还不支持fetch

您可以考虑为其添加一个 polyfill,它为您提供服务器端版本并为这些浏览器创建此功能。

$ npm install --save unfetch

然后将其导入您的项目中:

import fetch from 'unfetch' 使用 ES6

var fetch = require('unfetch') 使用 CommonJS 模块

【讨论】:

  • react-native 支持 fetch
  • @Revansiddh 谢谢,确实-我刚刚读过。也许这是在不支持它的测试套件中运行的?与原帖不清楚。
猜你喜欢
  • 1970-01-01
  • 2020-09-22
  • 2017-11-12
  • 1970-01-01
  • 2020-06-05
  • 2016-11-18
  • 2018-02-20
  • 2021-05-21
  • 1970-01-01
相关资源
最近更新 更多