我需要在重定向后检索数据(html页面代码)。
也就是说,通过Post将数据发送到特定的url,然后如果数据正确,页面将被重定向,当重定向完成后,我应该恢复页面的源代码并打印它。
但是我在错误的地方没有成功吗?
代码:
import * as React from 'react';
import { Text, View, StyleSheet, Button } from 'react-native';
import { Constants } from 'expo';
import { TextInput } from 'react-native-paper';
import Base64 from './Base64';
import cio from 'cheerio-without-node-native';
export default class App extends React.Component {
constructor() {
super();
this.state = {
ident: '',
pwd: '',
};
}
login() {
let { ident, pwd } = this.state;
const data = {
iliad: {},
};
let details = {
'login-ident': ident,
'login-pwd': pwd,
};
let formBody = [];
for (let property in details) {
let encodedKey = encodeURIComponent(property);
let encodedValue = encodeURIComponent(details[property]);
formBody.push(encodedKey + '=' + encodedValue);
}
formBody = formBody.join('&');
fetch('https://www.iliad.it/account/', {
method: 'POST',
redirect: 'manual',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: formBody,
})
.then(response => response.text())
.then(body => {
const $ = cio.load(body);
const results = body;
console.log(results);
})
.catch(error => {
console.error(error);
});
}
render() {
return (
<View style={styles.container}>
<Text style={styles.title}>Iliad</Text>
<View
style={{
backgroundColor: '#13a8f4',
borderRadius: 5,
padding: 5,
marginBottom: 5,
}}>
<TextInput
style={styles.textinput}
onChangeText={ident => this.setState({ ident })}
value={this.state.ident}
label="IdUser"
keyboardType="numeric"
theme={{ colors: { primary: '#03a9f4' } }}
/>
<TextInput
style={styles.textinput}
onChangeText={pwd => this.setState({ pwd })}
value={this.state.pwd}
label="Pwd"
theme={{ colors: { primary: '#03a9f4' } }}
/>
</View>
<Button onPress={() => this.login()} title="Login" color="#03a9f4" />
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
paddingTop: Constants.statusBarHeight,
backgroundColor: '#fff',
padding: 8,
},
title: {
margin: 24,
fontSize: 48,
fontWeight: 'bold',
textAlign: 'center',
color: '#c00',
},
textinput: {
/*height: 40,
borderColor: '#000',
borderWidth: 1,
padding: 5,
borderRadius: 5,
marginBottom: 5,*/
selectionColor: '#000',
//marginBottom: 50,
backgroundColor: '#fff',
},
});