【发布时间】:2018-06-02 13:30:33
【问题描述】:
我是在 React 中创建 GET 请求的新手。当有人在输入字段中输入 url 时,我正在尝试从 Instagram 内容中获取 media_id。有趣的是,我确实在 Inspector 中获得了以下 GET 请求的响应,但我不确定如何正确执行此操作。
以下是我的组件代码。
import React, { Component } from 'react';
export default class UrlInput extends Component {
constructor(props){
super(props);
this.state = {
term: '',
mediaid: ''
};
this.onInputChange = this.onInputChange.bind(this);
}
componentDidMount(){
const url = 'https://api.instagram.com/oembed/?url=http://instagram.com/p/Y7GF-5vftL/';
fetch(url).then(response => {
if(response.ok){
return response.json();
}
throw new Error('Request failed!');
},
networkError => console.log(networkError.message)).then(jsonResponse => {
return jsonResponse;
console.log(jsonResponse);
});
}
render(){
return (
<div className='search-bar'>
<input
value= {this.state.term}
onChange={ event => this.onInputChange(event.target.value)} />
<div>{this.state.term}</div>
</div>
);
}
onInputChange(term){
this.setState({term});
}
}
【问题讨论】:
-
你能发布 GET 调用的响应吗?如果您在
then中添加debugger;,您应该能够单步执行处理浏览器响应的代码。