0

I am doing the login throgh ajax. jquery function is working fine but its not going to the ajax url. So django view is not getting executed.

Ajax.html

$(function()
                {
                localStorage['domain'] = "http://122.172.64.142";
                var domain = localStorage['domain'];

                $('#fac1').on('click', function () {
                        var username = $("#username").val();
                        var password = $("#pwd").val();
                        data = {
                            name: username,
                            password: password
                            };

                        alert(domain);
                        $.ajax({
                            url: domain + "/login/login_android_here/",
                            type: "POST",
                            data: data,
                            success: function (response) {
                                                alert("success");
                                                window.location = 'file:///android_asset/www/posts.html';
                                                    },
                                        error: function () {
                                            alert('some error in login');
                                        }
                            });
                                return false;
                            });
                    });

My django views.py

@csrf_exempt
def login_android(request):
    print "i am in view"
    if request.method == "POST":
        print "you are in method"
        username = request.POST['name']
        password = request.POST['password']        
        login_api(request,username,password)
        #return HttpResponseRedirect('/home/')
        messages.success(request, 'You Loged In Successfully')
        response = json.dumps(username)
        return HttpResponse(response, mimetype="application/json")

When i click on login button i am getting alert but its not getting entered to view. Url is correct.

Wagh
  • 4,202
  • 5
  • 39
  • 62

3 Answers3

1

I would first recommend using Chrome with the developer tools console open. You could change you alerts for console.log().

When your trying window.location = 'file:///android_asset/www/posts.html'; You are trying to access a local resource. If I post that in my Chrome developer tools I get back Not allowed to load local resource: file:///android_asset/www/posts.html

If you would use window.location.replace("a url to your view"); this will work like a HTTP redirect. for more information redirect page

and you should be able to see your view.

Community
  • 1
  • 1
Eagllus
  • 437
  • 3
  • 8
0

I was made a silly mistake. I provided a wrong domain address on this page. Now it worked.

localStorage['domain'] = "http://122.172.64.142";

This address was wrong. Thats why it was not able to enter in to the view.

Wagh
  • 4,202
  • 5
  • 39
  • 62
-1

You forgot dataType ajax param

$.ajax({
    url: domain + "/login/login_android_here/",
    type: "POST",
    data: data,
    dataType : 'json', //dataType param IS MISSING
    success: function (response) {
                alert("success");
                window.location = 'file:///android_asset/www/posts.html';
                    },
        error: function () {
            alert('some error in login');
        }
});
xecgr
  • 5,095
  • 3
  • 19
  • 28
  • Still not working,,,,, i think dataType is not matter for this perticuler ajax. – Wagh Jul 14 '14 at 14:01
  • Try to change mimetype return to return HttpResponse(response, mimetype='application/javascript') – xecgr Jul 14 '14 at 14:10