我尝试使用此脚本进行调用,但收到“未捕获的语法错误:意外的 token :”
我不知道我哪里出了问题,我不知道这是否与我的回调有关。我正在发送一个有效的 JSON 数据包,所以我认为这不是问题所在。不管怎样,这是我的 html/jquery 调用:
<script>
function loadJSON(){
$.getJSON("http://localhost:8000/api/0.1/tonight-mobile.json&callback=?", function(data){
alert('hey');
$('result').append(data);
alert('hey 2');
$.each(data, function(i, json){});
});
alert('hey 3');
}
</script>
在我的 django View 中使用这个相应的函数:
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)
return HttpResponse(xml_bytes, content_type='application/json; charset=utf-8')
这是我的服务器记录的请求(所以我知道它至少能通过 OK 状态代码达到这一点):
"GET /api/0.1/tonight-mobile.json&callback=jsonp1293142434434 HTTP/1.1" 200 167
在我的 JavaScript 控制台中显示此错误:
Uncaught SyntaxError: Unexpected token : tonight-mobile.json&callback=jsonp1293142434434:1
如果您需要查看我的 JSON 格式(如果这就是导致此问题的原因),请告诉我,我也可以发布它。
预先感谢您的帮助!
更新:
这是来 self 的服务器的响应:
{"Places": [{"url": "http://localhost:8000/api/0.1/places/1.plist", "image_url": "http://localhost:8000/static/place_logos/Tsonoqua_Mask.gif", "name": "Boo's Place"}]}
[23/Dec/2010 17:37:22] "GET /api/0.1/tonight-mobile.json&callback=jsonp1293147441552 HTTP/1.1" 200 167
更新2:
我已经成功让我的回调正常工作了!代码如下:
客户端:
<script>
function loadJSON(){
$.getJSON("http://localhost:8000/api/0.1/tonight-mobile.json&callback=?", logIt(data));
}
function logIt(data){
window.console && console.log(data);
alert('yay!');
}
</script>
服务器端:
def tonight_mobile(request):
callback = request.GET.get('callback', 'logIt')
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)
if callback:
xml_bytes = '%s(%s)' % (callback, xml_bytes)
print xml_bytes
return HttpResponse(xml_bytes, content_type='application/javascript; charset=utf-8')
这将返回以下响应:
logIt({"Places": [{"url": "http://localhost:8000/api/0.1/places/1.plist", "image_url": "http://localhost:8000/static/place_logos/Tsonoqua_Mask.gif", "name": "Boo's Place"}]})
这就是它应有的功能吗?看来我应该能够简化 getJSON 调用...我只是想确保在继续向前解析响应并将它们设置为页面上的元素之前,我已经通过回调正确完成了所需的所有操作。
更新3*
所以,我已经取得了很大的进步!我使用的是 Jquery mobile,所以请忽略以下一些过度的 css,它与核心问题无关。
我在循环 JSON 数据包中的“地点”时遇到问题。我收到多个“地点”的响应,但似乎不知道如何迭代它们。我的每个循环中的“i”变量对于第一个元素正常工作,并显示其相应的名称和图像。
我的 getJSON 和回调方法已经演变成这样:
<script>
function loadJSON(){
$.getJSON("http://localhost:8000/api/0.1/tonight-mobile.json&callback=?", callback(data));
}
function callback(data){
$("#tonight-list").each(data.Places, function(i) {
$(this).append("<li role='option' tabindex='" + data.Places[i] + "' class='ui-li-has-thumb ui-li ui-btn ui-btn-icon-right ui-corner-top ui-corner-bottom ui-controlgroup-last ui-btn-down-c ui-btn-up-c' data-theme='c'><div class='ui-btn-inner ui-corner-top ui-corner-bottom ui-controlgroup-last'><span class='ui-icon ui-icon-arrow-r'></span><div class='ui-btn-text'><img src=" + data.Places[i].image_url + " alt=" + data.Places[i].name + " class='ui-li-thumb ui-corner-tl ui-corner-bl'/><h1 class='list-title ui-li-heading' id='list-title'><a href='detail.html?id=slide' data-transition='slide' class='ui-link-inherit'>" + data.Places[i].name + "</a></h1><span class='ui-li-count ui-btn-up-c ui-btn-corner-all'>" + data.Places[i].events + " events</span></div></div></li>");
});
}
</script>
这是我的回复:
callback({"Places": [{"url": "http://localhost:8000/api/0.1/places/3.plist", "image_url": "http://localhost:8000/static/place_logos/Bengals_1.png", "name": "Big 12", "events": 2}, {"url": "http://localhost:8000/api/0.1/places/2.plist", "image_url": "http://localhost:8000/static/place_logos/Makonde_Mask.gif", "name": "Harpo's", "events": 0}, {"url": "http://localhost:8000/api/0.1/places/1.plist", "image_url": "http://localhost:8000/static/place_logos/Quintons_1.png", "name": "Quinton's", "events": 1}]})
我是否对每个函数如何迭代 JSON(成为)Javascript 对象感到困惑?我很确定每个都是我的问题,因为我只得到“地点”列表的第一个元素。有人可以帮我吗?
最佳答案
是的,要返回 jsonp,您需要将响应包装在函数中:
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)
if callback:
xml_bytes = '%s(%s)' % (callback, xml_bytes)
return HttpResponse(xml_bytes, content_type='application/json; charset=utf-8')
关于使用 Django 回调的 Javascript getJSON 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4522952/