Jan Wilmar Jan Wilmar - 26 days ago 9
Ajax Question

SyntaxError: missing ; before statement on JSONResponse

I am getting this kind of error:

SyntaxError: missing ; before statement


I don't know what causes the error but I have this code here:

(function pollschedule(){
$.ajax({type: "GET",
dataType: "jsonp",
contentType: "application/json",
url: "http://127.0.0.1:8080/get/schedule/1/",
success: function(data){
console.log(data);
},
complete: pollschedule, timeout: 5000});
})();


In case you might need to see what http://127.0.0.1:8080/get/schedule/1/ is:

def get_schedule(request, sid):
schedule = Schedule.objects.filter(id=sid, date=datetime.datetime.now()).values('id', 'sched__name', 'date', 'time')
sched_collection = collections.defaultdict(list)
for i in schedule:
sched_collection[i['sched__name']].append(i)

return JsonResponse({"schedule" : dict(sched_collection)})


And returns this:

{"schedule": {"CWW": [{"date": "2016-11-11", "time": "17:10:10"}]}}


The error points the semi-colon after "schedule" in the response.

Answer

JSONP is not JSON. Your response is JSON, but you've told jQuery to expect JSONP.

Either make the response valid JSONP, or update the code doing the retrieval to expect JSON. (Note that if you do the latter, you will run into cross-origin issues unless the page you're running the ajax from is also on http://127.0.0.1:8080.)