首页 > Web开发 > 详细

fetch VS AJAX

时间:2016-04-19 06:05:30      阅读:187      评论:0      收藏:0      [点我收藏+]

fetch(‘https://mywebsite.com/endpoint/‘, { method: ‘POST‘, headers: { ‘Accept‘: ‘application/json‘, ‘Content-Type‘: ‘application/json‘, }, body: JSON.stringify({ firstParam: ‘yourValue‘, secondParam: ‘yourOtherValue‘, }) })

fetch("/data.json").then(function(res) {
if (res.ok) {
res.json().then(function(data) {
console.log(data.entries);
});
} else {
console.log("Looks like the response wasn‘t perfect, got status", res.status);
}
}, function(e) {
console.log("Fetch failed!", e);
});

‘Accept‘: ‘application/json‘,
‘Content-Type‘: ‘application/json

fetch("http://www.example.org/submit.php", {
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded;charset=UTF-8"
},
body: "firstName=Nikhil&favColor=blue&password=easytoguess"
}).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!");
});

fetch VS AJAX

原文:http://www.cnblogs.com/jayruan/p/5406461.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!