我try 将fetch与以下语法结合使用:

fetch("https://user:password@url", {
   ...
}).then((response) => {
   ...
}).done();

相同的url在CURL中工作,但在React Native中返回401.

有什么 idea 吗?

谢谢

推荐答案

我发现这种格式https://user:password@url在CURL和node中工作得很好,但在fetch中却不行.

我必须使用base-64NPM模块并通过Headers对象.

// https://www.npmjs.com/package/base-64
const base64 = require('base-64');

...

var headers = new Headers();
headers.append("Authorization", "Basic " + base64.encode("user:password"));

fetch("https://url", {
    headers: headers
  })
  .then((response) => { ... })
  .done();
`

React-native相关问答推荐

一旦我关闭应用程序,Json struct 就会发生变化

何时在 React Native 中使用辅助功能 Role='link'?

ReactNative:在所有组件和操作之间共享 sqlite 实例的最佳方法

应用程序主体尚未注册

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

如何渲染多行文本组件,行间有白色间隙

mapDispatchToProps:有什么意义吗?

当组件在react-native 中重新呈现时,动态不透明度不会改变

自定义原生基础的选项卡

React Native Expo StackNavigator 重叠通知栏

ScrollView bounce的 2 种不同背景 colored颜色

Android 依赖 'com.google.android.gms:play-services-stats' 的编译(16.0.1)和运行时(17.0.0)类路径有不同的版本

React Native:当我从 XCode 运行应用程序时看不到 console.logs

在 React-Native 中手动设置 opacity 的 onPress of TouchableOpacity 的音量

horizontal水平 FlatList 前后的间距

ReactNative TextInput 不可见 iOS

ListView 和 FlatList 有什么区别?

React-native iOS 不显示图像(pod 问题)

React Native 必须双击才能使 onPress 工作

如何在 React 中将函数与钩子绑定?