简体   繁体   English

将参数从AJAX传递到DJANGO

[英]Passing parameter from AJAX to DJANGO

I want to pass a parameter from javascript into the django views using window.location. 我想使用window.location将参数从javascript传递到django视图中。 In this case the variable 'query' which is the string 'hi'. 在这种情况下,变量“ query”是字符串“ hi”。 How would I go about doing this? 我将如何去做呢? Below is what isn't working :) yet everything works fine when I try without a parameter. 以下是无法正常工作的:)但是,当我尝试不使用任何参数时,一切都可以正常工作。 Help? 救命?

views.py views.py

def error(request, query):
    print(query)
    return render(request, 'error.html')

html HTML

$(document).ready(function(){
    $("#butt").click(function(){
        $.ajax({
            type: "get",
            url: 'profile',
            datatype:'json',
            data: {
                'profname': 'jilsmith',
            },
            success: function(data) {
                if(data.status == 0){ 
                    alert("error page");

                    var query = "hi";
                    window.location = data.url(query);
                }
                if(data.status == 1){ 
                    alert("profile page");
                    window.location = data.profile
                }
            }
        }); 
    });
});

You need to pass the variable on de DATA section from Ajax. 您需要在Ajax的de DATA部分传递变量。

$(document).ready(function(){
  $("#butt").click(function(){
var query = "hi";
           $.ajax({
           type: "get",
           url: 'profile',
           datatype:'json',
            data: {query: query, profname: 'jilsmith'},
              success: function(data) {
                if(data.status == 0){ 
             alert("error page");

             window.location = data.url(query);

             }
              if(data.status == 1){ 
             alert("profile page");
             window.location = data.profile
             }
           }

       }); 
  });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM