我正在Django编写我的第一个项目,现在我想使用jQuery创建一个Ajax请求来获取一些数据。问题是Ajax请求返回:
GET http://localhost:8000/ajax/teams_for_issue/?medIssue=MI6 404 (Not Found)
我更确定问题出在URL上,而且在此项目之前我已经多次弄错了URL。我的Ajax代码如下所示:
var medIssue = _this.issueSelector.val();
$.ajax({
url: '/ajax/teams_for_issue/',
data: {
'medIssue': medIssue
},
dataType: 'json',
success: function(data) {
_this.setTeams(data.teams)
}
});
这是我想发送答案的Django函数:
def teams_for_issue(request):
medIssue = request.GET.get("medIssue", none)
teams = Team.objects.filter(has_competence=medIssue)
data = {
"teams":teams
}
return JsonResponse(data)
我已定义以下网址
url(r'newpatient/', views.newpatient, name='newpatient'),
url(r'ajax/teams_for_issue/', views.teams_for_issue, name='teams_for_issue'),
任何关于我出错的地方都会有很多帮助:)
答案 0 :(得分:1)
在ajax请求中定义类型。
$.ajax({
url: '/ajax/teams_for_issue/',
type: "POST",
data: {
'medIssue': medIssue
},
dataType: 'json',
success: function(data) {
_this.setTeams(data.teams)
}
});
您的视图也应该从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)