fetch-post请求正确姿势

fetch发送post请求,不用url传参数

地址栏url传参数有尺寸限制,从2k-8k不等


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
fetch("http://www.example.org/submit.php", {
method: "POST",
headers: {
'Accept': 'application/json',
'Content-Type': 'application/x-www-form-urlencoded'
},
body: "userName=&password=admin"
}).then(function(res) {
if (res.ok) {
alert("Perfect! Your settings are saved.");
} else if (res.status == 401) {
alert("Oops! You are not authorized.");
}
}, function(e) {
alert("Error submitting form!");
});

图示