博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Javascript getJSON issue using Django callback
阅读量:6655 次
发布时间:2019-06-25

本文共 3796 字,大约阅读时间需要 12 分钟。

am trying to make a call with this script, but am getting a "Uncaught SyntaxError: Unexpected token :"

I can't figure out where I'm going wrong, I don't know if it's something with my callback. I am sending a valid JSON packet so I don't think that's the issue. Anyway, here's my html/jquery call:

With this corresponding function in my 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 inPlace.objects.all()]     )     xml_bytes = json.dumps(place_data)     returnHttpResponse(xml_bytes, content_type='application/json; charset=utf-8')

Here is my request being logged by my server (so I know its at least getting that far with a OK status code):

"GET /api/0.1/tonight-mobile.json&callback=jsonp1293142434434 HTTP/1.1"200167

Displaying this error in my javascript console:

UncaughtSyntaxError:Unexpected token : tonight-mobile.json&callback=jsonp1293142434434:1

If you need to see my JSON format (if that is what's causing this) please let me know and I can post it too.

Thank you in advance for your help!


UPDATE:

Here's the response coming from my server:

{
"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/201017:37:22]"GET /api/0.1/tonight-mobile.json&callback=jsonp1293147441552 HTTP/1.1"200167

UPDATE 2:

I've managed to get my callback working correctly! Here's the code:

Client side:

Server side:

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 inPlace.objects.all()]     )     xml_bytes = json.dumps(place_data)     if callback:         xml_bytes ='%s(%s)'%(callback, xml_bytes)     print xml_bytes     returnHttpResponse(xml_bytes, content_type='application/javascript; charset=utf-8')

This returns the following response:

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"}]})

Is this how it should function? It seems I should be able to simplify the getJSON call... I just want to make sure I've done everything I need to correctly with my callback before I continue forward parsing the response and setting them to elements on the page.


UPDATE 3*

So, I've made quite some progress!! I'm using Jquery mobile, so ignore some of the following overdone css, its not related to the core issue.

I'm having a problem looping over the "Places" in my JSON packet. I am getting a response with multiple "Places", but can't seem to figure out how to iterate over them. My 'i' variable in my each loop is working correctly for the first element, and displaying its corresponding name & image.

My getJSON and callback method have evolved into this:

Here's my response:

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}]})

Am I confusing how the each function iterates over the JSON (which become) Javascript objects? I am pretty sure the each is my issue here, as I am only getting the FIRST element of the "Places" list. Can someone please help me out?

转载地址:http://kdtto.baihongyu.com/

你可能感兴趣的文章
每天一个linux命令(36):diff 命令
查看>>
一个简单的算法_应该是最笨的写法了
查看>>
20120622第二天_面向对象\02面向对象
查看>>
[翻译].NET框架中的缓存
查看>>
Microsoft Visual Studio 2010 正式版下载[含旗舰版序列号](中、英文版)
查看>>
心惊胆战的多屏图片切换
查看>>
office excel读写类NPOI
查看>>
技术支持经验总结
查看>>
正则教程
查看>>
如何使用Exchange Web Service获取日历(包含循环会议)
查看>>
C# DEV XtraGrid
查看>>
【SAS NOTES】data set if
查看>>
关于C#的Process的内存相关属性解读
查看>>
Android 编程下快捷图标的创建
查看>>
C++ GUI Qt4 自学笔记——Qt qmake命令
查看>>
烂透了与棒极了
查看>>
修改10g自动统计信息收集作业GATHER_STATS_JOB到仅仅周末执行
查看>>
Calibrate测试Exadata IO
查看>>
【C语言】15-预处理指令1-宏定义
查看>>
【C语言】19-static和extern关键字1-对函数的作用
查看>>