React - 我无法从 fetch post 中看到我的回复
React - I can't see my response from fetch post
我不认为这需要绑定才能看到响应,但我一定遗漏了一些东西,因为我从这个 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',
});
})
);
}
post 看起来不错。它命中了我的 webApi,我得到了回复。我正在使用 fiddler 查看我的开发工作站上的 http 流量。这是来自我的 webApi 的提琴手中的响应消息。
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"}
当前 MYRESPONSE:: 控制台日志仅显示。
MYRESPONSE::[object Promise]
或"MYRESPONSE::"+JSON.stringify(响应)显示
MYRESPONSE::{}
.json()
returns一个承诺。你需要做 .json().then(data => console.log(data))
我不认为这需要绑定才能看到响应,但我一定遗漏了一些东西,因为我从这个 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',
});
})
);
}
post 看起来不错。它命中了我的 webApi,我得到了回复。我正在使用 fiddler 查看我的开发工作站上的 http 流量。这是来自我的 webApi 的提琴手中的响应消息。
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"}
当前 MYRESPONSE:: 控制台日志仅显示。
MYRESPONSE::[object Promise]
或"MYRESPONSE::"+JSON.stringify(响应)显示
MYRESPONSE::{}
.json()
returns一个承诺。你需要做 .json().then(data => console.log(data))