Jan Wilmar Jan Wilmar - 9 months ago 71
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: "",
success: function(data){
complete: pollschedule, timeout: 5000});

In case you might need to see what 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:

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 Source

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