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));
};
Fetch POST All in One的更多相关文章
- Git 少用 Pull 多用 Fetch 和 Merge
本文有点长而且有点乱,但就像Mark Twain Blaise Pascal的笑话里说的那样:我没有时间让它更短些.在Git的邮件列表里有很多关于本文的讨论,我会尽量把其中相关的观点列在下面. 我最常 ...
- git提示:Fatal:could not fetch refs from ....
在git服务器上新建项目提示: Fatal:could not fetch refs from git..... 百度搜索毫无头绪,最后FQgoogle,找到这篇文章http://www.voidcn ...
- sublime 插件推荐: Nettuts+ Fetch
Nettuts+ Fetch github地址:Nettuts-Fetch 在sublime中直接用 ctrl+shift+P -> pci -> Nettuts-Fetch 即可下载 这 ...
- git pull和git fetch的区别
Git中从远程的分支获取最新的版本到本地有这样2个命令:1. git fetch:相当于是从远程获取最新版本到本地,不会自动merge Git fetch origin master git log ...
- Hibernate之加载策略(延迟加载与即时加载)和抓取策略(fetch)
假设现在有Book和Category两张表,表的关系为双向的一对多,表结构如下: 假设现在我想查询id为2的那本书的书名,使用session.get(...)方法: Session session=H ...
- SQL Server 2012提供的OFFSET/FETCH NEXT与Row_Number()对比测试(转)
原文地址:http://www.cnblogs.com/downmoon/archive/2012/04/19/2456451.html 在<SQL Server 2012服务端使用OFFSET ...
- 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 我试着执行该存储过程,结果出现下面错误,每次执行该存储过程,得 ...
- Fetch:下一代 Ajax 技术
Ajax,2005年诞生的技术,至今已持续了 10 年.它是一种在客户端创建一个异步请求的技术,本质上它不算创新,是一组技术的组合.它的核心对象是 XMLHttpRequest. 简单回顾下历史 19 ...
- 在 JS 中使用 fetch 更加高效地进行网络请求
在前端快速发展地过程中,为了契合更好的设计模式,产生了 fetch 框架,此文将简要介绍下 fetch 的基本使用. 我的源博客地址:http://blog.parryqiu.com/2016/03/ ...
- 解决:error: Cannot fetch repo (TypeError: expected string or buffer)
同步源码,问题重现: Fetching project platform/external/libopus Fetching project repo error: Cannot fetch repo ...
随机推荐
- 138. Subarray Sum【Lintcode,by java】
Description Given an integer array, find a subarray where the sum of numbers is zero. Your code shou ...
- 项目开发设计模式理解之MVC模式
项目开发设计模式之MVC模式: M model 模型层 V view 视图层 C control 控制器 MVC模式在B/S架构下使用很广泛的软件设计模式,分成三个相对独立的模块构成,model+vi ...
- Mypwd 的解读与实现 20155202
Mypwd 的解读与实现 20155202 linux下pwd命令的编写 实验要求: 1 学习pwd命令 2 研究pwd实现需要的系统调用(man -k; grep),写出伪代码 3 实现mypwd ...
- 安装虚拟机以及学习Linux基础入门
安装虚拟机 参考基于VirtualBox虚拟机安装Ubuntu图文教程完成了虚拟机的安装,主要遇到了以下2个问题 在新建虚拟电脑的时候,如果类型选择了Linux,则版本就只能选择Ubuntu(32 位 ...
- logstash patterns github
USERNAME [a-zA-Z0-9._-]+ USER %{USERNAME} INT (?:[+-]?(?:[0-9]+)) BASE10NUM (?<![0-9.+-])(?>[+ ...
- GBDT为什么不能并行,XGBoost却可以
传统的GBDT是以CART作为基分类器,xgboost还支持线性分类器,这个时候XGBOOST相当于带L1和L2正则化的逻辑斯蒂回归(分类问题)或者线性回归(回归问题).传统的GBDT在优化的hih只 ...
- windows下如何将Python文件打包成.exe可执行文件
在使用Python做开发的时候,时不时会给自己编写了一些小工具辅助自己的工作,但是由于开发依赖环境问题,多数只能在自己电脑上运行,拿到其它电脑后就没法运行了.这显得很不方便,不符合我们的初衷,那么有没 ...
- mongo复杂操作
相比关系型数据库, Array [1,2,3,4,5] 和 Object { 'name':'DragonFire' } 是MongoDB 比较特殊的类型了 特殊在哪里呢?在他们的操作上又有什么需要注 ...
- mybatis 加载配置文件的方法
一. 使用sqlSessionFactory 的 mapperLocations 进行加载 <!-- SessionFactory --> <bean id="sqlSe ...
- cinder的组件
跟nova相似,cinder也有很多组件,每个组件负责各自的业务,然后共同协作完成volume的管理.组件之间的通信方式与nova个组件之间的通信方式相同,都是通过消息队列进行通信. cinder-a ...