无法通过任何可见错误到达AJAX成功事件(错误事件也不会触发(。我正在尝试将数据加载到<div id="list"></div>
块。打开页面后,控制台中只有一条消息:beforeSend
,没有关于成功或错误事件的信息。如何触发成功事件并填充此块?
views.py:
def load_api(request):
return render(request, 'bboard/api_rubrics.html')
@api_view(['GET'])
def get_api_rubrics(request):
if request.method == 'GET':
rubrics = Rubric.objects.all()
serializer = RubricSerializer(rubrics, many=True)
return Response(serializer.data)
urls.py:
urlpatterns = [
path('all_api_rubrics/', load_api),
path('all_api_rubrics/api/rubrics/', get_api_rubrics),
]
serializers.py:
class RubricSerializer(serializers.ModelSerializer):
class Meta:
model = Rubric
fields = ('id', 'name', 'order')
models.py:
class Rubric(models.Model):
name = models.CharField(max_length=20, db_index=True,
verbose_name='Name')
order = models.IntegerField(default=0, db_index=True)
def __str__(self):
return self.name
class Meta:
verbose_name_plural = 'Rubrics'
verbose_name = 'Rubric'
ordering = ['order', 'name']
api_rubrics.html:
{% load static %}
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<title>List of rubrics</title>
</head>
<body>
<div id="list"></div>
</body>
<script src="{% static 'bboard/rubrics1.js' %}"></script>
</html>
准则1.js:
window.onload = function() {
$.ajax({
method: 'GET',
url: 'api/rubrics',
beforeSend: function() {
console.log('beforeSend');
},
succsess: function(result) {
update_table(result);
console.log('after send');
},
error: function() {
console.log('error');
}
});
}
function update_table(data) {
console.log('inside update_table');
var list = document.getElementById('list');
var jsonData = JSON.parse(data);
var s = '<ul>';
for (i = 0; i < jsonData.length; i++) {
s += '<li>' + jsonData[i].name + '</li>';
}
s += '</ul>'
list.innerHTML = s;
}
我认为拼写成功可能是个错误。你能用正确的拼写检查一下吗。
success: function(result) {
}