0

Here is a sample response:

  200: {"timestamp": 1402348389.349506, "messages": {"b": "more not working", "c": "i get it already", "a": "not working"}}WTF???   

Here is the AJAX.

 this.pollForUpdates = function() {
            $.ajax({
                     type:"POST",
                     url:"{% url 'message_poller' %}",
                     data: {'lastmessage_received': this.last_received, 'address': this.getAddressJson() },
                     success : function(json) { doSomething(json)},
                     error : function(xhr,errmsg,err) {
                     alert(xhr.status + ": " + xhr.responseText + 'WTF???'); },
        });
        };

Return in view:

response = json.dumps({'messages': {"a": "not working", "b": "more not working", "c": "i get it already"}, "timestamp": time.time() }) 
    return HttpResponse(response, content_type='application/javascript') 

So I'm pretty much stumped here. All of my other AJAX POST calls work fine with the same HttpResponse.

user3467349
  • 3,043
  • 4
  • 34
  • 61

1 Answers1

0

To return for a JSON request:

return HttpResponse(response, content_type="application/json")

To return for a JSONP request:

return HttpResponse(response, content_type="application/javascript")
petkostas
  • 7,250
  • 3
  • 26
  • 29
  • I would agree if I didn't have 5 Ajax requests that are returning content_type="application/javascript" - wtf is the selective failure about? – user3467349 Jun 09 '14 at 22:15
  • return HttpResponse(json.dumps({'response': qr_code }), content_type='application/javascript') - for example that works for an Ajax request, I can't excactly post a big comment with the other views. – user3467349 Jun 09 '14 at 22:16
  • I don't know your code mate, this is just the difference between the 2, since json works for you (and regarding your view function that's how it should) then you need to check the rest of the views. – petkostas Jun 09 '14 at 22:16
  • From jquery documentation: dataType (default: Intelligent Guess (xml, json, script, or html)) Type: String The type of data that you're expecting back from the server. If none is specified, jQuery will try to infer it based on the MIME type of the response (an XML MIME type will yield XML, in 1.4 JSON will yield a JavaScript object, in 1.4 script will execute the script, and anything else will be returned as a string). The available types (and the result passed as the first argument to your success callback) are: – petkostas Jun 09 '14 at 22:17
  • Apparently specifying json in your datatype yields that this call expects json response, the default is intelligent guess, which means if you return a javascript response without a datatype defined jquery will interpret it as a script type. – petkostas Jun 09 '14 at 22:20
  • In short, change to application/json which is the correct format, it works for the other views as application/javascript because you force jquery to interpret the results as json. – petkostas Jun 09 '14 at 22:21
  • Okay I got it figured out. Basically the data has to be in the format {"response": data } for content_type="application/javascript", in which case it will be interpreted as JSON, otherwise it will return an error as I saw above. If you can add that note to your answer it might help someone in the future - it's really quite obscure. – user3467349 Jun 09 '14 at 22:23
  • Yes but do you understand the difference between jsonp and json? that's why you need to change the response type – petkostas Jun 09 '14 at 22:25
  • Actually it's it's the referal in the ajax, now I understand. If you don't set dataType: "json" in the AJAX it has to be specified as Json in the HttpResponse, – user3467349 Jun 09 '14 at 22:30
  • http://stackoverflow.com/questions/2887209/what-are-the-differences-between-json-and-jsonp – petkostas Jun 09 '14 at 22:31