【问题标题】:React Native using Cookies使用 Cookie 反应原生
【发布时间】: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


    【解决方案1】:

    好吧,您不能在移动环境中“触摸”cookie,至少这是我在 2 年前遇到同样问题时的想法。您不能 console.log cookie 值或类似的东西。但是,您可以在 fetch 方法中进行设置:

    credentials: 'same-origin'
    

    我不确定这是否对您有用,因为我使用的是相同的 API,我可以看到在您的情况下您想使用不同的 API,但我认为仍然值得一试!祝你好运。

    【讨论】:

    • 我应该把它放在两个 fetch 方法头上吗?还是直接登录?
    • 我为所有通话添加了这个。所以我有一个 fetch 模板,我用不同的 api url、数据等调用它。每次调用我都会将这一行添加到 fetch 中。
    • 是的,我认为它不起作用:(我无法从我的第二个 API 获取数据。我不认为 cookie 已“保存”并在新 api 中再次自动使用
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多