Fetch POST All in One
Fetch POST All in One
FPAIO
"use strict";
/**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms 2016-present
* @description Example POST method implementation:
* @augments
* @example
* @reference_link https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch
* @reference_link https://davidwalsh.name/fetch
* @reference_link https://www.w3schools.com/html/html_forms.asp
*/
const postData = (url = ``, data = {}) => {
// Default options are marked with *
return fetch(url, {
body: JSON.stringify(data), // must match "Content-Type" header
cache: "no-cache", // *default, no-cache, reload, force-cache, only-if-cached
credentials: "same-origin", // include, same-origin, *omit
headers: {
// "user-agent": "Mozilla/4.0 MDN Example",
"Content-Type": "application/json",
// "Content-Type": "text/plain",
// "Content-Type": "text/plain",
},
method: "POST", // *GET, POST, PUT, DELETE, etc.
mode: "cors", // no-cors, cors, *same-origin
redirect: "follow", // manual, *follow, error
referrer: "no-referrer", // *client, no-referrer
})
.then(response => response.json()) // parses response to JSON
.then(json => {
// json
console.log(`json =`, JSON.stringify(json, null, 4));
return json;
})
.catch(err => console.error(`error =`, err));
};
postData(
"http://example.com/answer",
{
answer: 37
}
).then(data => console.log(data)) // JSON from `response.json()` call
.catch(error => console.error(error));
// Promise & Fetch POST
const fetchPOST = (url = `http://10.1.64.116:8080/http/send/svnlog`) => {
let obj = {
"pro_name":"org.gil.sydb.server.report",
"class_names":["org.gil.sydb.server.report.hkstock.topic.company_finance.report.PerformanceForecastReport"]
},
query = `data=${JSON.stringify(obj)}`;
return fetch(
url,
{
method: "POST",
mode: "cors",
headers: {
"Content-Type": "application/json",
},
// body: `type=3&pid=10005&ucode=44003&id=18405&name=默认name&value=${json.items[0].value}`
body: JSON.stringify(query),
}
)
.then(res => res.json())
.then(
(json) => {
return json;
}
).catch(err => console.log(`fetch error`, err));
};
// JSON Object String
const FetchPOSTJSONObjectString = () => {
let arr = ["org.gil.sydb.server.report.hkstock.topic.company_finance.report.PerformanceForecastReport"],
query = `pro_name=org.gil.sydb.server.report&class_names=${JSON.stringify(arr)}`,
url = `http://10.1.64.116:8080/http/send/svnlog`;
return fetch(
url,
{
method: "POST",
mode: "cors",
headers: {
"Content-Type": "application/json",
},
body: query,
}
)
.then(res => res.json())
.then(
(json) => {
return json;
}
).catch(err => console.log(`fetch error`, err));
};
// Text String
const FetchPOSTText = () => {
let arr = ["org.gil.sydb.server.report.hkstock.topic.company_finance.report.PerformanceForecastReport"],
query = `pro_name=org.gil.sydb.server.report&class_names=${JSON.stringify(arr)}`,
url = `http://10.1.64.116:8080/http/send/svnlog`;
return fetch(
url,
{
method: "POST",
mode: "cors",
headers: {
"Content-Type": "text/plain",// text
},
body: query,
}
)
.then(res => res.json())
.then(
(json) => {
return json;
}
).catch(err => console.log(`fetch error`, err));
};
// Form String
const FetchPOSTFormString = () => {
let arr = ["org.gil.sydb.server.report.hkstock.topic.company_finance.report.PerformanceForecastReport"],
query = `pro_name=org.gil.sydb.server.report&class_names=${JSON.stringify(arr)}`,
url = `http://10.1.64.116:8080/http/send/svnlog`;
return fetch(
url,
{
method: "POST",
mode: "cors",
headers: {
"Content-Type": "application/x-www-form-urlencoded",// form default
},
body: query,
}
)
.then(res => res.json())
.then(
(json) => {
return json;
}
).catch(err => console.log(`fetch error`, err));
};
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/10191165.html
未经授权禁止转载,违者必究!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
2015-12-28 使用 ReStructuredText + Sphinx + Python 开发wiki ebooks!
2015-12-28 HTTP Status Code 451 All In One
2015-12-28 linux 必备 学习 资源 汇总 大全!
2015-12-28 drag and drop image testing!