0

I am a beginner and searched a lot about it but do not got any thing please explain why this error is coming.

I have used this code from here:

JavaScript post request like a form submit

Error is coming from the following code in my html template in under script tag:

This code is from Davidson Lima comment:

post("{% url 'savep' %}", {name: user_name, pass: user_pass, csrfmiddlewaretoken: $("#csrf_token").val()});

Here is the code of views.py

def save_login(request):
  x = request.POST['user']
  y = request.POST['pass']
  member = credentials(user_name=x, user_pass=y)
  member.save()

Error:


Forbidden (403)

CSRF verification failed. Request aborted.
Help

Reason given for failure:

    CSRF token from POST has incorrect length.
    
Abhay Kumar
  • 116
  • 1
  • 12

1 Answers1

0

It worked on firefox but not in google chrome,
We have to pass csrf_token that was stored at $(".csrf_token").context.cookie location

post("{% url 'savep' %}", {name: user_name., pass: user_pass, csrfmiddlewaretoken: $(".csrf_token").context.cookie.split('=')[1]});

See doc, In order to work it on all browsers: https://docs.djangoproject.com/en/4.0/ref/csrf/

Abhay Kumar
  • 116
  • 1
  • 12