其他分享
首页 > 其他分享> > VUE+Django前后端分离-第三部分【前后端数据传递】

VUE+Django前后端分离-第三部分【前后端数据传递】

作者:互联网

一、前端代码

首先:前端中任何变量都要被定义,具体如下:

 

 

 

<template>
    <div>
        <h3>推置引擎测试界面</h3>
        <el-form :inline="true" :model="formInline" class="demo-form-inline">
            <el-form-item label="异常id">
                <el-input v-model="formInline.exceptionid" placeholder="异常id"></el-input>
            </el-form-item>
            <el-form-item label="方案id">
                <el-input v-model="formInline.planid" placeholder="方案id"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="onSubmit">查询</el-button>
            </el-form-item>
        </el-form>

        <el-table :data="tableData" style="width: 90%">
            <el-table-column prop="formInline.rule" label="规则" width="1000">
            </el-table-column>
            <el-table-column prop="formInline.result" label="结果" width="180">
            </el-table-column>
        </el-table>
    </div>

</template>

<script>
    export default {
        data() {
            return {
                formInline: {
                    exceptionid: '',
                    planid: '',
                    rule: '',
                    result: '',
                },
                requestpara: {
                    exceptionid: '',
                    planid: '',
                }
            }
        },
        methods: {
            onSubmit() {
                this.requestpara.exceptionid = this.formInline.exceptionid
                this.requestpara.planid = this.formInline.planid
                this.$http.post('http://127.0.0.1:8000/calculate_testengine/',this.requestpara);
            }
        }
    }
</script>

<style>

</style>

二、后端代码

from django.http import JsonResponse
from django.views.decorators.http import require_http_methods

@require_http_methods(["POST"])
def testengine(request):
    print("这是个测试")
    print(request.POST)
    print(request.body)
    data = request.body.decode("utf-8")
    print("data",data)
    json_data =json.loads(data)
    exceptionid = json_data["exceptionid"]
    planid = json_data["planid"]
    response = {}
    try:
        response['msg'] = 'success'
        response['data'] = 'exceptionid 为{},plan_id为{}'.format(exceptionid,planid)
    except:
        response['msg'] = 'fail'
        response['data'] = 'exceptionid 不为{},plan_id不为{}'.format(exceptionid,planid)
    return JsonResponse(response)

三、展示效果

 

 

Request URL: http://127.0.0.1:8000/calculate_testengine/ Request Method:POST

Request Payload: {"exceptionid":"334668","planid":"AR220811000002"}

响应:

{     "msg": "success",     "data": "exceptionid 为334668,plan_id为AR220811000002" }

标签:VUE,http,requestpara,前后,planid,Django,exceptionid,data,response
来源: https://www.cnblogs.com/like1824/p/16589383.html