我一直在尝试调用服务器端POST函数来对Web应用程序中的用户进行身份验证。我的ExpressJS和ReactJS环境都已正确配置并运行,一个在端口3000上,一个在3001上。
我能够调用函数并执行服务器端Express代码,但是当我尝试访问React中的返回值时,该值似乎未定义。希望有帮助!
反应代码:
import React, { Component } from 'react';
export default class tem extends Component {
constructor(props) {
super(props);
var testVar = {
key: "0000000000",
status: false
};
this.state = {
users: [],
username: "",
password: "",
submitted: "yes",
responseData: testVar
};
this.handleNameChange = this.handleNameChange.bind(this);
this.handlePassChange = this.handlePassChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
this.getResponse = this.getResponse.bind(this);
}
handleNameChange(e) {
this.setState({ username: e.target.value });
}
handlePassChange(e) {
this.setState({ password: e.target.value });
}
getResponse(urlPath, user, pass) {
let setResp = this;
fetch(urlPath, {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
username: this.state.username,
password: this.state.password
})
})
.then(function(response) {
return response.json();
})
.then(function(response) {
setResp.setState({ responseData: response });
});
}
handleSubmit(event) {
event.preventDefault();
this.setState({ submitted: "YES" });
this.setState({
responseData: this.getResponse(
"/login",
this.state.username,
this.state.password
)
});
}
render() {
return (
<div>
<h1>login </h1>
Enter your username
<form onSubmit={this.handleSubmit}>
<input
type="text"
placeholder="Search..."
value={this.props.filterText}
onChange={this.handleNameChange}
/>
Enter your password
<input
type="password"
placeholder="enter password"
value={this.props.filterText}
onChange={this.handlePassChange}
/>
<button type="submit">Start</button>
</form>
{this.state.responseData.key}
{this.state.submitted}
</div>
);
}
}
我稍后尝试访问reponseData.key,但出现以下错误:
我输出的JSON是:
{
"key": "408f51qk54",
"status": true
}
我通过邮递员尝试过,并且工作正常。我不知道是什么错误!
答案 0 :(得分:1)
response.json()
返回一个Promise,因此在使用setState
中的响应之前,请确保在Promise链中将其返回。
let setResp = this;
fetch(urlPath, {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
username: this.state.username,
password: this.state.password
})
})
.then(function(response) {
return response.json();
})
.then(function(response) {
setResp.setState({ responseData: response });
});
您也不应该使用setState
的结果来调用this.getResponse
,因为您不会从this.getResponse
返回任何信息,并且它是异步的。这样会使您的responseData
成为undefined.
只需致电this.getResponse
:
handleSubmit(event) {
event.preventDefault();
this.setState({ submitted: "YES" });
this.getResponse(
"/login",
this.state.username,
this.state.password
);
}
答案 1 :(得分:0)
例如,您要像这样从.post返回数据:
app.post('/', function (req, res) {
// server side code
res.json(/*returning data*/);
});
因此,在React代码中,您可以这样检索它:
functionThatMakePostRequest = (args) => {
const data = {
id: 555,
name: "name",
};
fetch('/', {
method: 'post',
body: JSON.stringify(data),
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
})
.then(res => res.json()) //returns array of data
.then(res => this.setState({ res })); //assign state to array res
};