Express框架Fetch通信总结

本文总结了使用Fetch进行Express框架通信时遇到的问题及解决方法。重点在于理解fetch的配置,包括get通信、header设置、请求方式和body参数结构。由于不熟悉node.js和fetch,导致在接收参数时遇到困难,解决办法是引入body-parser中间件,让Express能解析请求参数。建议深入学习node文档以避免类似困扰。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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,
                        
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值