与 Day23 范例的差异,就是把 js fetch 的语法 extract 成 function
除了把原本的语法 extract 出来,额外再加上 .catch(),来达到基本的共用处理逻辑 !
window.CustomFetch = {};
CustomFetch.PostJson = function (url, data) {
return fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
RequestVerificationToken: Antiforgery.RequestVerificationToken,
},
body: JSON.stringify(data),
})
.then((response) => response.json())
.catch((error) => console.log(error));
};
引用 CustomFetch.js
因为 CustomFetch 里面会呼叫 Antiforgery 物件资料
依照我个人习惯
我会把 CustomFetch 的引用,放在 引用 Antiforgery 的下方 !
...
<partial name="_Antiforgery" />
<script src="/lib/CustomFetch.js?20210608001"></script>
...
把原本 fetch 的语法,改为呼叫 CustomFetch.PostJson(url, request_body).then() 的语法
...
<script>
const app = createApp({
setup(){
...
const calculate = function () {
CustomFetch.PostJson(calculate_url, vue_model.value)
.then(data => update_vue_model(data));
}
...
const submit_form = function() {
CustomFetch.PostJson(post_url, vue_model.value)
.then(data => update_vue_model(data));
}
...
有兴趣的人,也可以试试改用 partial view 的方式,来呼叫 js 共用元件
就像 Antifogery 那样 !
这篇先到这里,下一篇来看看整合 jQuery UI 的部份 !
<<: Component 鬼牌(二): 看网址决定 Component
有些功能性的标签不需要内文,写法如下: < b >粗体字< /b > b 标...
中国银行长城跨境通卡,产品全称中国银行长城跨境通国际借记卡,可能是目前唯一能够自由办理的国际借记卡。...
有些公司永远在徵人(人员一直在流动),实际去应徵过後,会深刻理解到为什麽。 前几天提到GitHub时...
现在开始建立介面前有一个重要的事情要做 就是单纯的 console.log 到画面上少了很多科技感 ...
哈罗!昨天使用 SWR 实作了一个小功能,让使用者可以列出某 Github user 的所有公开 r...