因此,我尝试使用 jQuery .getJSON 对本地 Web 服务器进行简单调用,并使用 python/django 来满足其请求。使用的地址是:
http://localhost:8000/api/0.1/tonight-mobile.json?callback=jsonp1290277462296
我正在尝试编写一个简单的 Web 视图,可以访问此 url 并返回 JSON 数据包作为结果(稍后担心实际元素值/布局)。
这是我仅警告/返回数据的简单尝试:
$.getJSON("http://localhost:8000/api/0.1/tonight-mobile.json&callback=?",
function(json){
alert(json);
<!--$.each(json.items, function(i,item){
});-->
});
我可以直接访问此 URL,无论是 http://localhost:8000/api/0.1/tonight-mobile.json 或 http://localhost:8000/api/0.1/tonight-mobile.json&callback=jsonp1290277462296 并取回有效的 JSON 数据包... 所以我假设它在我的 noob javascript 中:)
生成此响应的views.py 函数如下所示:
def tonight_mobile(request):
callback = request.GET.get('callback=?', '')
def with_rank(rank, place):
return (rank > 0)
place_data = dict(
Places = [make_mobile_place_dict(request, p) for p in Place.objects.all()]
)
xml_bytes = json.dumps(place_data)
xml_bytes = callback + '(' + xml_bytes + ');'
return HttpResponse(xml_bytes, mimetype="application/json")
使用相应的 urls.py 配置:
(r'^tonight-mobile.json','iphone_api.views.tonight_mobile'),
我仍然对如何使用回调感到有些困惑,所以也许这就是我的问题所在。请注意,我可以直接调用给我响应的“blah.json”文件,但不能通过有线 URL 调用。有人可以帮助我指导吗?
So, I'm trying to make a simple call using jQuery .getJSON to my local web server using python/django to serve up its requests. The address being used is:
http://localhost:8000/api/0.1/tonight-mobile.json?callback=jsonp1290277462296
I'm trying to write a simple web view that can access this url and return a JSON packet as the result (worried about actual element values/layout later).
Here's my simple attempt at just alerting/returning the data:
$.getJSON("http://localhost:8000/api/0.1/tonight-mobile.json&callback=?",
function(json){
alert(json);
<!--$.each(json.items, function(i,item){
});-->
});
I am able to access this URL directly, either at http://localhost:8000/api/0.1/tonight-mobile.json or http://localhost:8000/api/0.1/tonight-mobile.json&callback=jsonp1290277462296 and get back a valid JSON packet... So I'm assuming it's in my noob javascript:)
My views.py function that is generating this response looks as follows:
def tonight_mobile(request):
callback = request.GET.get('callback=?', '')
def with_rank(rank, place):
return (rank > 0)
place_data = dict(
Places = [make_mobile_place_dict(request, p) for p in Place.objects.all()]
)
xml_bytes = json.dumps(place_data)
xml_bytes = callback + '(' + xml_bytes + ');'
return HttpResponse(xml_bytes, mimetype="application/json")
With corresponding urls.py configuration:
(r'^tonight-mobile.json','iphone_api.views.tonight_mobile'),
I am still somewhat confused on how to use callbacks, so maybe that is where my issue lies. Note I am able to call directly a 'blah.json' file that is giving me a response, but not through a wired URL. Could someone assist me with some direction?
发布评论
评论(1)
首先,
callback = request.GET.get('callback=?', '')
不会获取callback
的值。效果好多了。
去调试这种事情。您可能希望在 Django 视图函数中包含
print
语句,以便您可以看到发生了什么。例如:print repr(request.GET)
放在视图函数中是很有帮助的,这样您就可以看到 GET 字典。First,
callback = request.GET.get('callback=?', '')
won't get you the value ofcallback
.Works much better.
To debug this kind of thing. You might want to include
print
statements in your Django view function so you can see what's going on. For example:print repr(request.GET)
is a helpful thing to put in a view function so that you can see the GET dictionary.