关于feth提交数据
来源:20-4 使用 React Router 4 重写项目(三)
慕先生3252166
2017-06-14
老师,我想问一下fetch后面那个url,也就是后端的 API后面带的参数可不可以不跟在后面,而是把它单独提出来像 ajax的data传参一样,那应该怎么做呢?另外fetch后面的url 也可以单独提出来吗
写回答
1回答
-
慕先生3252166
提问者
2017-06-14
fetch("http://www.example.org/submit.php", { method: "POST", headers: { "Content-Type": "application/x-www-form-urlencoded" }, body: "firstName=Nikhil&favColor=blue&password=easytoguess"}).then(function(res) { if (res.ok) { console.log("Perfect! Your settings are saved."); } else if (res.status == 401) { console.log("Oops! You are not authorized."); }}, function(e) { console.log("Error submitting form!");});我看阮一峰老师是这么用body传参的,我想问可不可以json或者数组。另外我们课程的fetch 的两个then的作用和区别是什么呢
00
相似问题