FLATeric
FLATeric

Reputation: 91

ajax post returns 404 for url in django 1.10

Im trying to post a simple ajax to 'signup' function in django but keep getting 404, I think it has to do with my url dispatcher and I need some help please.. my ajax looks like this:

$('#signupBtn').click(function() {
$.ajax({
    type: 'POST',
    url : '/signup',
    dataType: "json",
    data : {
        userId : $('#signupEmail').val(),
        userPassword : $('#signupPassword').val()
    },
    beforeSend: function() {
        $('#signupBtn').hide();
    },
    error: function(xhr,errmsg,err) {
        $('#signupBtn').show();
        $('#signupEmail').val("");
        $('#signupPassword').val("");
        alert(xhr.status);
      },
    success : function(json) {
            $('#signupBtn').show();
            $('#signupEmail').val("");
            $('#signupPassword').val("");
            alert(json['u']);
    }
});
  return false;});

my views.py lookes like this:

def signup(request):
    userid = request.POST.get('userId')
    userpass = request.POST.get('userPassword')
    data = { 'u' : userid, 'p' :  userpass}
    return JsonResponse(data)

and my app urls lookes like this:

from django.conf.urls import url
from . import views

urlpatterns = [
    url(r'^$', views.index, name='index'),
    url(r'^signup$', views.signup, name='signup'),]

what is my problem? and how do i fix it? thanks.

Upvotes: 0

Views: 250

Answers (2)

FLATeric
FLATeric

Reputation: 91

SOLVED! should have indid put an extra / in the end.

Upvotes: 0

miketery
miketery

Reputation: 121

Two things could be going wrong.

  1. Missing CSRF token in API call. https://docs.djangoproject.com/en/1.11/ref/csrf/
  2. Not properly serialized data. You need to use data=JSON.stringify({dictionary});

Upvotes: 1

Related Questions