URL_Flip
URL_Flip

Reputation: 163

Ajax request to Django returns 404 not found

I am writing my first project in Django where I now want to make an Ajax request using jQuery to get some data. The problem is that the Ajax request returns:

 GET http://localhost:8000/ajax/teams_for_issue/?medIssue=MI6 404 (Not Found)

I am rather certain that the problem is with the URL, and I have gotten the URLs wrong several times before in this project. My Ajax code looks as follows:

var medIssue = _this.issueSelector.val();
$.ajax({
    url: '/ajax/teams_for_issue/',
    data: {
        'medIssue': medIssue
    },
    dataType: 'json',
    success: function(data) {
        _this.setTeams(data.teams)
    }
});

This is the Django function that I want to send the answer:

def teams_for_issue(request):
    medIssue = request.GET.get("medIssue", none)
    teams = Team.objects.filter(has_competence=medIssue)
    data = {
        "teams":teams
    }
    return JsonResponse(data)

I have defined the following URL

url(r'newpatient/', views.newpatient, name='newpatient'),
url(r'ajax/teams_for_issue/', views.teams_for_issue, name='teams_for_issue'),

Any help on where I go wrong would be much appriciated :)

Upvotes: 1

Views: 2692

Answers (1)

badiya
badiya

Reputation: 2257

define type in your ajax request.

$.ajax({
   url: '/ajax/teams_for_issue/',
   type: "POST",
   data: {
     'medIssue': medIssue
 },
 dataType: 'json',
 success: function(data) {
     _this.setTeams(data.teams)
}
});

also your view should read data from request.POST

def teams_for_issue(request):
    medIssue = request.POST.get("medIssue", none)
    teams = Team.objects.filter(has_competence=medIssue)
    data = {
       "teams":teams
   }
    return JsonResponse(data)

Upvotes: 1

Related Questions