1. 程式人生 > 其它 >django和ajax實現傳值

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')