Browse Source

接收post的json内容,塞入数据库

master
xyiege 5 months ago
parent
commit
35673d0916
  1. 53
      aufs/core/setting.go
  2. 7
      aufs/db/servDb.go
  3. 8
      vue/afvue/src/utils/request.js

53
aufs/core/setting.go

@ -3,7 +3,6 @@ package core
import (
"aufs/db"
"encoding/json"
"fmt"
"net/http"
"strconv"
)
@ -59,18 +58,44 @@ func Scedit(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
} else {
// 设置响应头为JSON格式
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Access-Control-Allow-Origin", "*") // 允许跨域(生产环境需指定具体域名)
w.Header().Set("Access-Control-Allow-Methods", "POST, OPTIONS")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
// 处理预检请求
if r.Method == http.MethodOptions {
w.WriteHeader(http.StatusOK)
return
}
// 解析表单数据
// r.ParseForm()
// 接受json 数据
var scinfo db.StServerInfo
err := json.NewDecoder(r.Body).Decode(&scinfo)
if err != nil {
// 解析失败返回错误信息
errorResp := map[string]string{"error": "JSON格式错误", "details": err.Error()}
json.NewEncoder(w).Encode(errorResp)
return
}
defer r.Body.Close()
// 处理接收的json
// 数据库初始化
db.Init()
// 编辑
db.UpdateServerInfo(scinfo)
// 输出
scresp := ScJson{
Message: "success",
Status: 200,
Data: []db.StServerInfo{scinfo},
}
uCorsHadler(w, r)
json.NewEncoder(w).Encode(scresp)
}
// 解析表单数据
frmdata := r.ParseForm()
fmt.Println(frmdata)
// 获取参数
// id := frmdata.Get("id")
// // 转换为整数
// scid, err := strconv.Atoi(id)
// if err != nil {
// // 处理转换错误
// }
// 数据库初始化
db.Init()
// 编辑
}

7
aufs/db/servDb.go

@ -89,19 +89,18 @@ func GetServerInfo(id int16) StServerInfo {
}
// 更新服务器信息
func UpdateServerInfo(info *StServerInfo) error {
func UpdateServerInfo(info StServerInfo) {
// 更新语句
stmt, err := db.Prepare("UPDATE s_info SET addr = ?, port = ?, token = ? WHERE id = ?")
if err != nil {
return err
panic(err)
}
defer stmt.Close()
// 执行更新语句
_, err = stmt.Exec(info.Addr, info.Port, info.Token, info.Id)
if err != nil {
return err
panic(err)
}
return nil
}
// 删除服务器信息

8
vue/afvue/src/utils/request.js

@ -1,8 +1,6 @@
import axios from 'axios'
// import store from '@/store'
// import storage from 'store'
// import notification from 'ant-design-vue/es/notification'
// import message from 'ant-design-vue/es/message'
import { VueAxios } from './axios'
import { isObject } from './util'
// import { ACCESS_TOKEN } from '@/store/mutation-types'
@ -53,11 +51,7 @@ service.interceptors.response.use((response) => {
}, (error) => {
// 网络请求出错
const errMsg = ((error.response || {}).data || {}).message || '请求出现错误,请稍后再试'
notification.error({
message: '网络请求出错',
description: errMsg,
duration: 3
})
console.log(errMsg)
return Promise.reject(error)
})

Loading…
Cancel
Save