我不认为这需要绑定才能看到响应,但我一定遗漏了一些东西,因为我从这个 Fetch/post 得到了响应。这是我的收获。
export default class Test extends Component {
constructor(props) {
super(props);
this.state = { value: '' };
this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}
componentDidMount() { }
handleChange(event) {
this.setState({ value: event.target.value });
}
handleSubmit(event, cb) {
event.preventDefault();
return (
fetch('test/post', 'POST')
.then(response => {
if (response.status >= 400) {
this.setState({
value: 'error',
});
throw new Error('Throw Error');
}
console.log('REACT::RESPONSE', response.json());
return response.json();
})
.then(cb)
.catch(() => {
this.setState({
value: 'error cb',
});
})
);
}
Run Code Online (Sandbox Code Playgroud)
帖子看起来不错。它击中了我的 webApi,我得到了回复。我使用 fiddler 查看我的开发工作站上的 http 流量。这是我的 webApi 中 fiddler 中响应消息的样子。
HTTP/1.1 200 OK
Date: Wed, 08 Feb 2017 22:49:27 GMT
Content-Type: application/json; charset=utf-8
Server: Kestrel
Access-Control-Allow-Origin: *
Content-Length: 16
{"name":"MyName"}
Run Code Online (Sandbox Code Playgroud)
目前 MYRESPONSE:: 控制台日志只显示。
MYRESPONSE::[object Promise]
Run Code Online (Sandbox Code Playgroud)
或 "MYRESPONSE::"+JSON.stringify(response) 显示
MYRESPONSE::{}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1174 次 |
| 最近记录: |