electron发送post请求

参考官网地址:(https://www.electronjs.org/zh/docs/latest/api/net)
src/http.ts

ts 复制代码
import { net } from 'electron';
// ----------网络请求封装--
/**
 * POST请求数据接口
 * @param api 接口地址,如:'http://192.168.1.110/user/login'
 * @param data 请求数据,JOSN 格式,或 object 或 string
 */
export function sendPOST(api:string,data:JSON|object|string){
sendPOST_ASYNC(api,data)
  .then(response => {
    console.log('POST 请求成功: ',response);
    return response;
  }).catch(err => {
    console.log('POST 请求异常: ',err);
    return null;
  })
}
/**
 * GET请求数据接口
 * @param api 接口地址,如:'http://192.168.1.110/ping'
 */
export function sendGET(api:string){
  sendGET_ASYNC(api)
  .then(response => {
    console.log('GET 请求成功: ',response);
    return response;
  }).catch(err => {
    console.log('GET 请求异常: ',err);
    return null;
  })
}
/**
 * POST请求数据接口 - 异步接口
 * @param api 接口地址,如:'http://192.168.1.110/user/login'
 * @param data 请求数据,JOSN 格式,或 object 或 string
 */
async function sendPOST_ASYNC(api:string,data:JSON|object|string){
  const request:RequestInit = {
   method:'POST',
   body:JSON.stringify(data),
   headers:{'Content-Type':'application/json'}
  }
  const response = await net.fetch(api,request)
  if (response.ok) {
    const body = await response.json()
    return body
  }
}
/**
 * GET请求数据接口 - 异步接口
 * @param api 接口地址,如:'http://192.168.1.110/ping'
 */
async function sendGET_ASYNC(api:string){
  const response = await net.fetch(api)
  if (response.ok) {
    const body = await response.json()
    return body
  }
}

main.ts 使用

ts 复制代码
import { sendPOST,sendGET } from './http';
sendPOST('http://192.168.1.110/user/login',{username:"xxx",pwd:"xxx"});
sendGET('http://192.168.1.110/ping')
相关推荐
小小小小宇13 分钟前
前端国际化看这一篇就够了
前端
大G哥16 分钟前
PHP标签+注释+html混写+变量
android·开发语言·前端·html·php
whoarethenext18 分钟前
html初识
前端·html
小小小小宇30 分钟前
一个功能相对完善的前端 Emoji
前端
m0_6278275231 分钟前
vue中 vue.config.js反向代理
前端
Java&Develop32 分钟前
onloyoffice历史版本功能实现,版本恢复功能,编辑器功能实现 springboot+vue2
前端·spring boot·编辑器
白泽talk36 分钟前
2个小时1w字| React & Golang 全栈微服务实战
前端·后端·微服务
摆烂工程师44 分钟前
全网最详细的5分钟快速申请一个国际 “edu教育邮箱” 的保姆级教程!
前端·后端·程序员
HhhDreamof_1 小时前
云贝餐饮 最新 V3 独立连锁版 全开源 多端源码 VUE 可二开
前端·vue.js·开源
Simaoya1 小时前
【vue】【element-plus】 el-date-picker使用cell-class-name进行标记,type=year不生效解决方法
前端·javascript·vue.js