【发布时间】:2019-07-16 08:35:04
【问题描述】:
我已经创建了一个登录页面和一个今日页面,并且我正在尝试实现 cookie。当我登录时,我使用用户名/密码,它从 API 返回一个 cookie。我现在想通过我通过登录检索到的 cookie 从另一个 api 获取数据。我如何在 react native 中传递 cookie?
我的 login.js 页面如下所示。
async login() {
try {
let response = await fetch('http://192.168.168.114:8090/Cam/LoginOut/role', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
role: this.state.role,
username: this.state.username,
password: this.state.password
})
});
let res = await response.text();
if(response.status >= 200 && response.status < 300) {
let headers = response.headers
let newCookie = headers['Set-Cookie'];
console.log(response.headers);
console.log("res is + " + res);
this.props.navigation.navigate('Idag');
} else {
let errors = res;
console.log(response.status + " fejl, Jeg er næsten inde");
throw errors;
}
} catch (errors) {
console.log('There has been a problem with your fetch operation: ');
// ADD THROW ERRORS EVENTUALLY
}
}
我的 today.js 页面抓取看起来像这样
componentDidMount = () => {
try {
let response = fetch('http://192.168.168.114:8090/Cam/Calendar/show', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
from : "2019-02-20 08:00:00",
to : "2019-02-21 17:00:00"
})
});
let res = response.text();
if(response.status >= 200 && response.status < 300) {
//data fra svar her
console.log("Today: res is + " + res);
} else {
let errors = res;
console.log("Today: Jeg er næsten inde");
throw errors;
}
} catch (errors) {
console.log('Today: There has been a problem with your Data: ');
//this.dropdown.alertWithType('error', 'Error', error.message);
//this.props.navigation.navigate('TodayScreen');
// ADD THROW ERRORS EVENTUALLY
}
};
它的组件确实安装在我的 today.js 中,所以它会在用户登录后立即加载。(登录有效,我有一个导航器可以切换到其他页面)
【问题讨论】:
标签: reactjs react-native cookies fetch jsx