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));
};

Fetch POST All in One的更多相关文章

  1. Git 少用 Pull 多用 Fetch 和 Merge

    本文有点长而且有点乱,但就像Mark Twain Blaise Pascal的笑话里说的那样:我没有时间让它更短些.在Git的邮件列表里有很多关于本文的讨论,我会尽量把其中相关的观点列在下面. 我最常 ...

  2. git提示:Fatal:could not fetch refs from ....

    在git服务器上新建项目提示: Fatal:could not fetch refs from git..... 百度搜索毫无头绪,最后FQgoogle,找到这篇文章http://www.voidcn ...

  3. sublime 插件推荐: Nettuts+ Fetch

    Nettuts+ Fetch github地址:Nettuts-Fetch 在sublime中直接用 ctrl+shift+P -> pci -> Nettuts-Fetch 即可下载 这 ...

  4. git pull和git fetch的区别

    Git中从远程的分支获取最新的版本到本地有这样2个命令:1. git fetch:相当于是从远程获取最新版本到本地,不会自动merge Git fetch origin master git log ...

  5. Hibernate之加载策略(延迟加载与即时加载)和抓取策略(fetch)

    假设现在有Book和Category两张表,表的关系为双向的一对多,表结构如下: 假设现在我想查询id为2的那本书的书名,使用session.get(...)方法: Session session=H ...

  6. SQL Server 2012提供的OFFSET/FETCH NEXT与Row_Number()对比测试(转)

    原文地址:http://www.cnblogs.com/downmoon/archive/2012/04/19/2456451.html 在<SQL Server 2012服务端使用OFFSET ...

  7. Attempt to fetch logical page (...) in database 2 failed. It belongs to allocation unit xxxx not to xxx

    今天一个同事说在一个生产库执行某个存储过程,遇到了错误: Fatal error 605 occurred at jul 29 2014 我试着执行该存储过程,结果出现下面错误,每次执行该存储过程,得 ...

  8. Fetch:下一代 Ajax 技术

    Ajax,2005年诞生的技术,至今已持续了 10 年.它是一种在客户端创建一个异步请求的技术,本质上它不算创新,是一组技术的组合.它的核心对象是 XMLHttpRequest. 简单回顾下历史 19 ...

  9. 在 JS 中使用 fetch 更加高效地进行网络请求

    在前端快速发展地过程中,为了契合更好的设计模式,产生了 fetch 框架,此文将简要介绍下 fetch 的基本使用. 我的源博客地址:http://blog.parryqiu.com/2016/03/ ...

  10. 解决:error: Cannot fetch repo (TypeError: expected string or buffer)

    同步源码,问题重现: Fetching project platform/external/libopus Fetching project repo error: Cannot fetch repo ...

随机推荐

  1. mybash的编写与调试

    fork() 计算机程序设计中的分叉函数.返回值: 若成功调用一次则返回两个值,子进程返回0,父进程返回子进程标记:否则,出错返回-1. fork函数将运行着的程序分成2个(几乎)完全一样的进程,每个 ...

  2. 20155224 2016-2017-2 《Java程序设计》第4周学习总结

    20155224 2016-2017-2 <Java程序设计>第4周学习总结 教材学习内容总结 第六章 第六章主要学习了子类与父类的继承. 先定义一个程序,另一程序可继承他 如: publ ...

  3. 实现Linux下dc的功能,计算后缀表达式的值

    提交测试截图和码云练习项目链接,实现Linux下dc的功能,计算后缀表达式的值 -将运算符写在两个操作数之后的表达式称为"后缀表达式",如上面的中缀表达式可转换为后缀表达式1 2 ...

  4. thinkphp查询:

    $Role=D('role'); //查询数据表 $role_data = $Role->order('role_id')->group('role_name')->select() ...

  5. 通过unixODBC访问PostgreSQL数据库

    磨砺技术珠矶,践行数据之道,追求卓越价值回到上一级页面:PostgreSQL基础知识与基本操作索引页    回到顶级页面:PostgreSQL索引页[作者 高健@博客园  luckyjackgao@g ...

  6. 【MongoDB】如何注册windows服务

    一.为什么要注册windows服务 mongodb启动比较麻烦,每次都要cmd去开启.注册windows服务,可以设置开机启动,比较友好. 二.如何注册windows服务 1.安装mongodb 2. ...

  7. day 7 引用

    1.b=a在c语言和python中的区别 c语言:a=100  a变量里面放的100 b = a    b变量里面也放的100 python :  a=100   内存中有个100    a放的100 ...

  8. replace与replaceAll的区别

    这两者有些人很容易搞混,因此我在这里详细讲述下. replace的参数是char和CharSequence,即可以支持字符的替换,也支持字符串的替换(CharSequence即字符串序列的意思,说白了 ...

  9. 在CentOS 7下编译安装Nginx+PHP+MySQL环境

    本文转载自:http://www.softeng.cn/?p=156,本文已获得作者授权,未经作者同意,不可转载. 1.前言 本文适合于已经对Linux操作系统具有基本操作经验,并且能够在Linux或 ...

  10. 2017年4月8日Office 365 使用CSV文件导入邮件组

    国内版 第一步: Import-Module msonline Connect-MsolService 输入用户名密码 第二步: Get-MsolUser" 第三步: Set-Executi ...