import React from 'react';
import ReactDOM from 'react-dom';
import axios from 'axios';
//FormData和Payload是浏览器传输数据给接口的其中两种格式,这两种方式浏览器是通过Content-Type来进行区分的,如果是application/x-www-form-urlencoded,则为formdata方式,如果是application/json方式,则为payload方式。需要转换成formdata方式,则要下载qs进行转换
import qs from 'qs'
axios.post('/user-server/login/userLogin',qs.stringify({
userName:me.refs.name.value,
password:me.refs.password.value
})).then(res => {
console.log(res);
})
代码是用的backbone和zepto,主要是content-type,还需要使用JSON对象进行转化一下。
class FetchDemo extends React.Component {
constructor(props) {
super(props);
this.state = {
posts: []
};
}
componentDidMount() {
axios.get(`http://www.reddit.com/r/${this.props.subreddit}.json`)
.then(res => {
const posts = res.data.data.children.map(obj => obj.data);
this.setState({ posts });
});
}
render() {
return (
<div>
<h1>{`/r/${this.props.subreddit}`}</h1>
<ul>
{this.state.posts.map(post =>
<li key={post.id}>{post.title}</li>
)}
</ul>
</div>
);
}
}
ReactDOM.render(
<FetchDemo subreddit="reactjs"/>,
document.getElementById('root')
);