我试图使用ajax从ajax API中获取一些信息。我能够发布数据,但无法从服务器获得所有数据。我是Django环境中的新手,虽然我尝试过一些参考资料。I希望使用我提供的API从服务器获取数据,并使用ajax调用显示这些数据。
我所遵循的引用:
3.https://www.sourcecodester.com/tutorials/python/11762/python-django-simple-crud-ajax.html
我的get呼叫代码:
$.ajax({
type: "GET",
url: "/save_composition",
dataType: "json",
success: function(data) {
alert(data)
},
error: function(xhr, textStatus) {
alert("error..");
}});网址部分:
path('restore_composition/', views.restore_composition, name='restore_composition')Views.py:
def restore_composition(request):
data = SaveComposition.objects.all()
return render(request, 'index.html', context={'data': data})发布于 2019-10-18 12:01:37
在Django框架中,ajax调用就是这样工作的。
def ajax_method():
return HttpResponse(200)ajax调用的url
path('save_composition', views.ajax_method, name='ajax_method')您需要设置“`url path”而不需要正斜杠。
Ajax调用
$.ajax({
type: "GET",
url: "save_composition",
dataType: "json",
success: function(data) {
alert(data)
},
error: function(xhr, textStatus) {
alert("error..");
}});https://stackoverflow.com/questions/58450273
复制相似问题