django和ajax实现传值

输入框1 id:moa_req
输入框2 id:moa_rsp
前端代码

index.html
$.ajax({
        url: "/ctest/interface/",
        type: "post",
        data: {
            "pb": pb,
            "ip": ip,
            "clienttype": clienttype,
        },
#成功后返回data
        success: function (data) {
            // alert("请求成功");
#将返回的参数填入输入框
            $("#moa_req").val(data.req);
            $('#moa_rsp').val(data.rsp);

            //window.location.replace("/ctest/moauditool/");
        },
        error: function () {
            alert("error!失败咯失败咯");
        }
    });

django部分代码

url.py
from xxx import interface
path('interface/',interface.interface),

返回参数

interface.py

def interface(request):
    if request.method == 'POST':
        moa_pb = request.POST["moa_pb"]
        moa_ip = request.POST["moa_ip"]
        clienttype = request.POST["clienttype"]

        req = exeResult(moa_pb,moa_ip,clienttype,True)
        moa_pb = switchPb(moa_pb)
        rsp = exeResult(moa_pb,moa_ip,clienttype,True)
        data = {"req": req, "rsp": rsp}
#这一步很重要,将字典转为json,传值回去
        data = json.dumps(data)
        # print(data)
        return HttpResponse(data,content_type='application/json')

 

posted @ 2021-12-10 17:51  sugoi  阅读(147)  评论(0)    收藏  举报