0

I call

$.getJSON('http://localhost:8000/polls/?callback=?', function(data)

and recieve an error:

Error: parsererror errorThrown: jQuery16103397698865741826_1320825997345 was not called

If I call a json file that is next to my html file it works fine. If I call the google example url: http://www.highcharts.com/samples/data/jsonp.php?filename=aapl-c.json&callback=? it also works.

Any ideas? I think that maybe it has something to do with DJango or the way I return from my server side code.

index(request):

    a = {}
    a[1320675940] = 1.8
    a[1320675941] = 2.8
    a[1320675942] = 38
    a[1320675943] = 4.8
    a[1320675944] = 5.8
    a[1320675945] = 6.8
    a[1320675946] = 7.8

    data = simplejson.dumps(a)

    return HttpResponse(data, mimetype="text/plain")
    #return HttpResponse(data, mimetype="json")
    #return HttpResponse(str(data), mimetype="text/plain")
second
  • 28,029
  • 7
  • 75
  • 76
AYBABTU
  • 986
  • 3
  • 17
  • 39
  • 1
    See http://stackoverflow.com/questions/3076414/ways-to-circumvent-the-same-origin-policy – tylerl Nov 09 '11 at 08:18
  • @tylerl I'm not convinced that this is the problem since the google url works and it is obviously not in my domain. I am not a web developer so I'm a bit lost here. – AYBABTU Nov 09 '11 at 08:23
  • Do you get [valide JSON](http://jsonlint.com/) if you call `http://localhost:8000/polls/?callback=?` directly? – PiTheNumber Nov 09 '11 at 08:32
  • @PiTheNumber - yes. Ran the validator and it is valid – AYBABTU Nov 09 '11 at 08:40
  • I think the JSON is not the focus here. It is the Django script I wrote. Since google returns a JSON-P format. That fails the validations of the site you reffered me. If this was the problem I'd get a JSON parse error - I think this error is for parsing the reponse packet. – AYBABTU Nov 09 '11 at 08:43

1 Answers1

2

You are returning JSON (with a plain text content-type), not JSON-P.

You need to look at the value of callback in the query string and wrap the JSON in that function call.

e.g. for ?callback=foo:

foo(/* Your JSON here */);

You should also use the correct content type (application/javascript).

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335