- 此问题在此处已有答案**:
POST Request with Fetch API?(7个答案)
4天前关闭。
我正在reactjs中尝试通过fetch
进行POST请求。我查看了一些文档,但错误没有解决。有人能帮我吗?
下面是我的reactjs代码:
handleSubmit(e) {
e.preventDefault();
var self = this;
const payload = {
id: 111,
studentName: 'param',
age: 24,
emailId: 2
};
fetch({
method: 'POST',
url: 'http://localhost:8083/students',
body: payload,
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
}
})
.then(function(response) {
return response.json()
}).then(function(body) {
console.log(body);
});
}
}
如果有人熟悉reactjs,就举一个简单的例子来说明如何调用post request,可以使用fetch
,也可以使用axios
。
2条答案
按热度按时间ehxuflar1#
这是一个例子。
6psbrbz92#
我刚才回答了一个类似的问题。here。
React最棒的地方在于它只是Javascript。
因此,所有您需要的是一些做POST做您的服务器!
您可以使用本机fetch函数或axios之类的完整库
使用两者的示例如下: