0

我一直在使用其他代码示例成功地将数据从我的 url 传递到我的视图,所以我不确定为什么这个有什么不同,但这就是我对视图的 ajax 调用所拥有的。我正在尝试传递 id 和 depth 的可选参数:

链接/urls.py

urlpatterns += patterns('links.ajax',
     url(r'^ajax/(?P<id>\d+)*$', 'ajax_graph_request', name='ajax_graph_request'),
)

链接/ajax.py

import json
from django.http import HttpResponse, HttpResponseRedirect

def ajax_graph_request(request, id):
   depth = request.GET.get('depth','1') 
   result = {'id':id, 'depth':depth}
   data = json.dumps(result)
   return HttpResponse(data, mimetype='application/json')

请求

console.log(record);
$.getJSON("/ajax/", { id:record, depth:2 }).done(function( data ){
    console.log(data);
});

响应

22145 (from js console print)
{"depth": "2", "id": null}

所以请求被正确地传播到正确的视图,但变量不是。为什么是这样?

4

1 回答 1

2

您的 URL 模式是ajax/(?P<id>\d+),视图ajax_graph_request需要一个id 作为参数。通过将其作为数据参数发送{ id:record, depth:2 }。它作为 akwarg而不是参数传递id

.getJson将方法更改为

$.getJSON("/ajax/"+record, { depth:2 }).done(function( data )

它会工作得很好。

于 2013-05-22T02:07:55.777 回答