Express框架Fetch通信
最近自己弄个博客站点,前台用的React,服务器用的是node实现的,node是第一次接触,所以还在摸索,这篇mark下通信时遇到的坑。
fetch配置:
window.fetchUtility = function (options, errorFun) {
var request = {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
// headers: {
// 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
// 'Accept': 'application/json'
// },
cache: 'no-store',
body:`userName=${options.data.userName}&password=${options.data.password}`
};
if (request.method.toLowerCase() === "get") {
request.body = null;
}
return fetch(options.url, request)
.then(function (response) {
if (response.ok) {
if (request.download) {
return response;
}
else {
return response.text().then(function (dataString) {
return {
responseStatus: response.status,
responseString: dataString,
isParseJson: request.isParseJson,
isPassStatus: request.isPassStatus
};
});
}
} else {
if (response.status == 403) {
window.location.href = "/error/" + response.status;
} else if (response.status == 409) {
// for simulation
$$.alert(true, { type: "w", content: "Sorry, currently you are in simulation mode and limited to read only access." });
throw new Error("simulation");
}
else {
if (errorFun) {
errorFun(response);
}
else {
throw new Error(response.statusText);
}
}
}
}).then(function (fetchResult) {
if (request.download) { return fetchResult };
var queryResult = null;
try {
if (!fetchResult.responseString) {
return null;
}
if (fetchResult.isParseJson && fetchResult.responseString) {
if ($.isEmptyObject(fetchResult.responseString)) {
queryResult = "";
} else {
queryResult = JSON.parse(fetchResult.responseString);
if (fetchResult.isPassStatus) {
queryResult[FetchResponsePropName.status] = fetchResult.responseStatus;
}
}
} else {
queryResult = fetchResult.responseString;
}
}
catch (ex) {
$$.error("An error happened while fetching information. Error:", ex);
}
return queryResult;
});
};
GET通信使用:
retrieve(){
let option = {
url: `./api/about/getAbout?test=${'dqhan'}`,
method:'Get'
}
fetchUtility(option).then(res=>{
var a = res;
}).catch(e=>{
console.log(e);
})
}
express接受参数形式:

POST通信:
postRequest() {
let data = {
params: { test: 'test' }
};
let option = {
url: `./api/about/postRequest`,
method: 'Post',
data: data
}
fetchUtility(option).then(res => {
var a = res;
}).catch(e => {
console.log(e);
})
}
因为调试过程中express中接受参数时一直接收不到,所以mark下(node小白,正在努力ヾ(◍°∇°◍)ノ゙)
问题原因:
对node的不熟悉,以及对fetch的不精通。
前后台通信数据传递最基本的结构:
- header定义发送、接受的媒体类型
- 请求方式post、get等等
- body参数结构
以上三点是最基本的参数,然而我一直在纠结是不是还有什么配置错误,于是一直在这里打转转。
问题根本原因是需要在node里面使用body-parser来接受参数,这样express才能解析通信发过来的参数。
解决方法:
var bodyParser = require('body-parser');
const app = new express();
app.use(bodyParser.urlencoded({extended: true}))
总结:
我应该好好看看node的文档。sorry~
Express框架Fetch通信的更多相关文章
- Node.js、Express框架获取客户端IP地址
Node.js //传入请求HttpRequest function getClientIp(req) { return req.headers['x-forwarded-for'] || req.c ...
- Win8.1 安装Express 框架
1.安装Windows Node.js客户端 2.安装Express框架 我本机是Win8.1的,使用命令npm install -g express安装Express,安装完成后显示一些安装明细,刚 ...
- Node.js Express 框架学习
转载:http://JavaScript.ruanyifeng.com/nodejs/express.html#toc0 感觉很牛的样子,不过觉得对初学者没太大用,里面很多例子用的api都没有详细的说 ...
- Node.js Express 框架
Node.js Express 框架 Express 简介 Express 是一个简洁而灵活的 node.js Web应用框架, 提供了一系列强大特性帮助你创建各种 Web 应用,和丰富的 HTTP ...
- express框架路由配置及congtroller自动加载
express框架在node官方推荐的一个框架,关于如何入门的文章,已经很多了,我就不在累赘了,本文的核心是如何修改文件使得更接近一个MVC的框架 express原生是通过require的方式实现了模 ...
- nodejs学习笔记二:解析express框架项目文件
上一章介绍了如何去创建一个express框架的工程项目,这章介绍一下express框架下的文件和用法解析,上一张我们创建的工程项目结构图如下: models是不属于原工程项目结构,为了实现数据模型后添 ...
- nodejs学习笔记一:安装express框架并构建工程目录
偶遇node是在一个阳光明媚的上午,无意间打开博客看到一片关于nodejs的介绍,通读全篇后,心情跌宕起伏,哎呀,这么好的东西我竟然现在才知道,这是最气的,于是马上开始制定学习nodejs计划,好了, ...
- Express框架使用以及数据库公共操作类整理(Win7下的NodeJs)
具体步骤: 1.安装开发工具WebStorm: 2.安装node/npm(下载地址:https://nodejs.org/download/)选择适合你的xxx.mis安装: 3.安装express框 ...
- express 框架之session
一.什么是session? 最近在学习node.js 的express框架,接触到了关于session方面的内容.翻阅了一些的博客,学到了不少东西,发现一篇博文讲的很好,概念内容摘抄如下: Sessi ...
随机推荐
- chrome安装HTTP测试扩展
chrome安装HTTP测试扩展 扩展名: DHC
- 修改urllib2源代码,定制User-Agent,一劳永逸
我经常用到urllib2这个库,基本上每次都要添加 User-Agent 为一个模拟浏览器的值. 突然想到,能不能直接修改源代码,添加 User-Agent 的值. google 到 https:// ...
- Kafka vs RocketMQ—— Topic数量对单机性能的影响
引言 上一期我们对比了三类消息产品(Kafka.RabbitMQ.RocketMQ)单纯发送小消息的性能,受到了程序猿们的广泛关注,其中大家对这种单纯的发送场景感到并不过瘾,因为没有任何一个网站的业务 ...
- struts学习笔记
------struts in action 读书笔记 1. ActionServlet:Struts 的ActionServlet控制导航流.当ActionServlet从容器接到一个请求,它使用U ...
- 基于jQuery Tooltips悬停提示效果
基于jQuery Tooltips悬停提示效果.这是一款基于jquery.tooltipster插件实现的jQuery Tooltips Hover effect特效.效果图如下: 在线预览 源码 ...
- spark练习题
site1,user1,-- :: site1,user2,-- :: site1,user3,-- :: site1,user3,-- :: site2,user4,-- :: site3,user ...
- 【C#】使用MySql.Data.dll连接MySQL数据库
准备工作 在Visual Studio中新建一个C#的控制台应用程序. 确保后台已经启用了MySQL57服务. 在MySQL的安装路径下找到DLL,默认位置是 C:\Program Files (x8 ...
- [加密]展讯secureboot方案
Secure Boot方案介绍及实施流程 转自网络 1. Secure boot概述 本文档主要是secure boot方案的介绍和说明,其内容会涵盖以下方面:secure boot的目的和介绍.技术 ...
- Android——黑名单管理(二)
说明:多加一点完善 1.在操作类 DAO 将连接数据库也放到了构造方法中(错误),加删除全部的方法 2.主界面增加了姓名一栏,用layout文件将对话框分离出来 3.删除加确认提示 4.加删除全部按钮 ...
- Face Alignment by Coarse-to-Fine Shape Searching--解析
人脸关键点定位.Face Alignment by Coarse-to-Fine Shape Searching 算法源码详解(上) http://blog.csdn.net/shenxiaolu19 ...