相关疑难解决方法(0)

React JS-如何通过获取语句对凭据进行身份验证

我的目标是创建一个运行JSON Rest服务的React JS登录页面。在Postman中,当我输入服务的URL时,将其设置为以POST身份运行,然后在正文中输入以下JSON:{用户名:“ myUserName”,密码:“ myPassword”} ...返回令牌。因此,在我的fetch子句中,我使用JSON.stringify将用户名和密码传递给服务器。

我是刚开始使用Fetch和react的,所以我的问题是,我如何开始对各种用户进行身份验证,而仅将React JS和fetch一起使用?我想,我要在Fetch子句的第二个语句中编写逻辑?

当前,我的页面接受任何凭据,并在单击“提交”按钮后将用户引导到登录页面。我有一个包含fetch的函数,现在在单击onSubmit按钮后立即调用fetch函数,该按钮现在捕获令牌。

这是我的代码:

import React, { Component } from 'react';
import ReactDOM from 'react-dom';
import './Login.css';
import { withRouter } from 'react-router-dom';

class Login extends Component {

    constructor() {
        super();
        this.state = {
            data: [],
            username: "",
            password: "",
            token: "",
        };
    } //end constructor

    componentWillMount() {
    }

    componentDidMount() {
        this.fetchData();
    }

    fetchData() {
        fetch('http://theapi/api/auth', {
            method: 'POST',
            headers: {
                'Content-type': 'application/json',
            },
             body: JSON.stringify({
                username: 'myUserName',
                password: 'myPassword',
                Authorization: 'TheReturnedToken',
            })
        }) …
Run Code Online (Sandbox Code Playgroud)

javascript api rest json reactjs

5
推荐指数
1
解决办法
9128
查看次数

标签 统计

api ×1

javascript ×1

json ×1

reactjs ×1

rest ×1