为了账号安全,请及时绑定邮箱和手机立即绑定

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 关注
  • 115 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号