【发布时间】:2017-08-07 09:03:02
【问题描述】:
我有一个反应原生的测试应用程序,当我远程启用调试 js 时一切正常。运行后,它在设备(来自 XCode)和模拟器中运行良好:
react-native run ios
问题是,如果我停止远程 js 调试,登录测试不再起作用。登录逻辑非常简单,我正在获取一个 api 来测试登录,API 端点是通过 https。
我需要改变什么?
更新:此代码在启用 JS Debug Remote 的情况下完美运行,如果我禁用它,它将不再工作。
/**
* Sample React Native App
* https://github.com/facebook/react-native
* @flow
*/
import React, { Component } from 'react'
import {
AppRegistry,
StyleSheet,
View,
Button,
Alert
} from 'react-native'
export default class MyClass extends Component {
constructor (props) {
super(props)
this.testFetch = this.testFetch.bind(this)
}
async testFetch () {
const email = 'email@example.com'
const password = '123456'
try {
const response = await fetch('https://www.example.com/api/auth/login', {
/* eslint no-undef: 0 */
method: 'POST',
headers: {
'Accept': 'application/json' /* eslint quote-props: 0 */,
'Content-Type': 'application/json',
'Authorization': 'Basic ' + btoa(email + ':' + password)
}
})
Alert.alert('Error fail!', 'Fail')
console.log(response)
} catch (error) {
Alert.alert('Error response!', 'Ok')
}
}
render () {
return (
<View style={styles.container}>
<Button
onPress={this.testFetch}
title="Test me!"
/>
</View>
)
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF'
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10
},
instructions: {
textAlign: 'center',
color: '#333333',
marginBottom: 5
}
})
AppRegistry.registerComponent('testingReactNative', () => MyClass)
谢谢。
【问题讨论】:
-
您至少需要在此处添加一些代码。 JS远程调试不太可能出现这个错误。
-
嗨@zvona 我已经用代码更新了这个问题......谢谢。
-
好的,我的错误是“btoa”在没有调试的情况下执行时未定义......但是为什么呢? :)
-
哦,很好。
atob和btoa在没有调试器的情况下无法工作(无法解释原因)。 -
和这个问题一样,你将不允许使用一些 es6 特性 react native 不支持但 chrome 支持,当你将 react-native 应用程序连接到 chrome 远程调试器时,你会更改 js 运行时环境到 chrome。这就是为什么你不能在没有远程调试器的情况下使用某些功能,而 polyfill 是解决方案。
标签: react-native