[英]Retrieving returned POST data in ReactJS
我一直在尝试调用服务器端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
}
我通过Postman尝试了它,它工作正常。 我无法弄清楚错误是什么!
response.json()
返回一个promise,因此在使用setState
的响应之前,请确保在promise链中返回该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
);
}
例如,您将从.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
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.