Using jquery ajax POST method with django

96 views
Skip to first unread message

Hossein Rashnoo

unread,
Jan 17, 2015, 12:46:32 AM1/17/15
to django...@googlegroups.com

I use this code to send a request:

function checkuser() {
    $.ajax({
        url: 'http://10.252.84.159/ajaxrecivelogin',
        //type: 'POST',
        data: "{'username': 'aa', 'password' : 'bb'}",
        context: this,
        dataType: 'json',
        success: function (data) {
            $("#resp").html("success");
        },
        error: function ()
        {
            $("#resp").html("error");
        }
    });
};

And its worked But when i add type:POST part it cause error.

I use this django code to response that request:

def ajaxrecivelogin(request):
        import json
        response_data = {}
        response_data['status'] = '1'
        return HttpResponse(json.dumps(response_data), content_type="application/json")

Is it about my django code?

Erwin Sprengers

unread,
Jan 17, 2015, 8:37:56 AM1/17/15
to django...@googlegroups.com
Hello,

POST works fine for me,  I use the following django code :

at the end of the view :

return HttpResponse(simplejson.dumps(response_dict), mimetype='application/javascript')

and following ajax code :

       $.ajax({
          url: '/ajax/is_key_mm/',
          type: 'POST',
          async:   false,
          data: {node : node.id, slug : "{{article_slug}}"},
          success: function(Result) {
                 if (Result.substring(1,4)=="NOK") {
                        console.log("not key");
                        key_label = "----";
                 }
                }
          });


Vijay Khemlani

unread,
Jan 17, 2015, 9:22:18 AM1/17/15
to django...@googlegroups.com
Maybe it's triggering the CSRF validation? What error message are you getting exactly from the server?

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/006eb711-5f5d-4903-a91b-c68e39a45d29%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Hossein Rashnoo

unread,
Jan 17, 2015, 11:44:00 PM1/17/15
to django...@googlegroups.com

I correct my code and it's worked. Thank you guys for your help.

Ajax Code:

function checkuser() {
    var myObject = new Object();
    myObject.username = $('#username').val();
    myObject.password = $('#password').val();
    $.ajax({
        url: 'http://10.252.84.159/ajaxrecivelogin/',
        type: 'POST',
        data: JSON.stringify(myObject),

        context: this,
        dataType: 'json',
        success: function (data) {

            alert( data.status );
        },
        error: function ()
        {
            alert( "Error" );
        }
    });
};

Django Code:

def ajaxrecivelogin(request):
        import json
        import ast
        x = ast.literal_eval(request.body)
        response_data = {}
        response_data['status'] = username
        
return HttpResponse(json.dumps(response_data), content_type="application/json")

And i comment out django.middleware.csrf.CsrfViewMiddleware in setting

carlos

unread,
Jan 21, 2015, 12:47:43 PM1/21/15
to django...@googlegroups.com
if you comment middleware csrf this is a risk security problem in your app
maybe you need read this part of documentation


Cheers

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
Reply all
Reply to author
Forward
0 new messages