亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

jquery post方法所需的Javascript函數

jquery post方法所需的Javascript函數

臨摹微笑 2023-03-18 15:04:35
我的查詢 let name = document.getElementById("Name").value;  let code = document.getElementById("Code").value;  let type = document.getElementById("ProductTypeId").value;$.ajax({   url: "/Home/jQueryAddComment",   type: "POST",   dataType: "html",   data: json,   success: function(data){       //var message = data.Message;       alert(data);      $('.CommentSection').html(data);   }}我需要使用 fetch 將我的 jQuery 轉換為純 Javascript 函數。它是一個帶有html返回的post方法。是否可以使用 fetch.編輯:下面是我的 Javascript 的 get 方法:    fetch(url + "?" + o)        .then(function (response) {            //check if it is redirected to custom error page            if (response.redirected && response.url.indexOf("/Error") > 0) {                response.text().then(function (html) {                    debugger                    window.location.href = response.url;                });            } else {                response.text().then(function (html) {                    document.getElementById('view-all').innerHTML = html;                });            }        })        .catch(function (err) {            console.log('Fetch Error', err);        });
查看完整描述

1 回答

?
RISEBY

TA貢獻1856條經驗 獲得超5個贊

fetch還接受第二個參數,它是一個文字對象,包含與請求有關的信息——例如請求方法、模式、標頭和 POST BODY。


直接來自 Mozilla 文檔:


var data = { name: "justin", code: "SDF", type: "some-type" };


fetch(url, {

   method: 'POST', // *GET, POST, PUT, DELETE, etc.

   mode: 'cors', // no-cors, *cors, same-origin

   cache: 'no-cache', // *default, no-cache, reload, force-cache, only-if-cached

   credentials: 'same-origin', // include, *same-origin, omit

   headers: {

      'Content-Type': 'application/json'

      // 'Content-Type': 'application/x-www-form-urlencoded',

   },

   body: JSON.stringify(data) // body data type must match "Content-Type" header

});

https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch


查看完整回答
反對 回復 2023-03-18
  • 1 回答
  • 0 關注
  • 106 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號