我打算fetch获得数据后实现直接登录,但一直报错显示undefined is not a function(evaluating 'this.login')



  • fetch("http://192.168.1.105:8080/app/v1/login", {
    method: "POST",
    mode: "cors",
    headers: {'Content-Type': 'application/json',},
    body: JSON.stringify({
    username: this.state.inputedPN,
    password: this.state.inputedPW,
    })
    }).then(function (res) {
    // console.log("fetch request ", JSON.stringify(res.ok));
    console.log('ssss');
    if(res.ok){
    res.json().then(function (json) {
    //console.info(json);
    //alert('提示 '+'来自后台:'+json.token);
    console.log('get successfully!');
    this.login();
    });
    //console.log(json.token);
    //admin=json.token;

            }else{
                alert('提示 '+'请求失败');
            }
    
        }).catch(function (e) {
            //console.log("fetch fail");
            alert('提示 '+'系统错误');
        });


  • function改用箭头函数


登录后回复