今晚我第一次修改Javascript/JQuery、AJAX、JSON和GeoDjango,我完全搞砸了!在
到目前为止,我已经能够通过POST请求成功地从AJAX提交一个搜索查询,在Django中执行搜索,然后将序列化的数据传回AJAX。但是,我不知道如何用AJAX再次解码它!在
我收到这个错误:(Uncaught TypeError: Cannot read property 'length' of undefined)
当我添加以下尝试处理数据时:$.each (json.results, function (i, val) {
//Loop through each returned item and push onto name
name.push(val.id);
//Loop through each returned item and push name onto var zip
zip.push(val.name);
});
我认为这就是相关的Ajax:
^{pr2}$
我的观点是以防万一:from django.shortcuts import HttpResponse
from django.views.generic import TemplateView
from django.core.serializers import serialize
from AlmondKing.Events.models import Event
from django.core.exceptions import SuspiciousOperation
class MapView(TemplateView):
template_name = "index2.html"
def post(self, request, *args, **kwargs):
if request.POST['event_search']:
self.object = Event.objects.search(request.POST['event_search'])
return HttpResponse(serialize('geojson',self.object), content_type="application/json")
else:
raise SuspiciousOperation("Blank Request Received")
样本输出:b'{"type": "FeatureCollection", "crs": {"type": "name", "properties": {"name": "EPSG:4326"}}, "features": [{"properties": {"tags": [], "name": "Party", "zip": "19146"}, "geometry": null, "type": "Feature"}, {"properties": {"tags": [], "name": "Jamboree", "zip": "19146"}, "geometry": null, "type": "Feature"}]}'
我对如何帮助AJAX理解数据感到困惑。我使用POST以便在基于类的TemplateView中执行函数。然而,POST的AJAX设置似乎不能很好地解释序列化的响应。我怎么能一个请求就完成这些呢?在