import React from 'react';
import ReactDOM from 'react-dom';
import axios from 'axios';
//FormData和Payload是瀏覽器傳輸數(shù)據(jù)給接口的其中兩種格式症汹,這兩種方式瀏覽器是通過Content-Type來進行區(qū)分的瓮下,如果是application/x-www-form-urlencoded房交,則為formdata方式朋鞍,如果是application/json方式棋凳,則為payload方式。需要轉(zhuǎn)換成formdata方式坠陈,則要下載qs進行轉(zhuǎn)換
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對象進行轉(zhuǎn)化一下。
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')
);