跳转至

2-Django前后端.md

前端发送表单

前端发送-01

function modifyx(data, url) {
    // 发送POST请求
    fetch(url, {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json'
        },
        body: JSON.stringify(data)
    })
        .then(response => response.json())
        .then(responseData => {
            showmsg("修改成功");
        })
        .catch((error) => {
            showmsg("修改失败");
        });
}

document.getElementById('dynamic-form').addEventListener('submit', function (event) {
    event.preventDefault(); // 阻止表单默认提交行为

    // 获取表单数据
    const formData = new FormData(this);
    const url = this.action;
    const data = {};
    formData.forEach((value, key) => {
        data[key] = value;
    });
    modifyx(data, url);
});

//=====================================
function submit(showlog = true) {
    // 获取表单数据
    // 获取表单数据

    const form = document.getElementById('dynamic-form');
    const formData = new FormData(form);
    const url = form.action;
    const data = {
        "key": 'task',
    };
    formData.forEach((value, key) => {
        data[key] = value;
    });
    modifyx(data, url, showlog);
}

后端接受-01

1
2
3
4
5
6
7
import json
from django.http import JsonResponse
def saveaiconf(request):
    x = request.body
    info = json.loads(x)
    remark = info.get("remark")
    return JsonResponse({"success":'更新成功'})