javascript - AJAX Not Loading JSON Object -
i'm trying simple request acronym finder api reason json not being returned. here code:
$.ajax('http://www.nactem.ac.uk/software/acromine/dictionary.py?sf=dod', { crossdomain:true, datatype: "jsonp", success: function(data){ console.log(data); } });
if visit url directly in browser, can see requested json, chrome console returns:
resource interpreted script transferred mime type text/plain: "http://www.nactem.ac.uk/software/acromine/dictionary.py?sf=dod&callback=jquery1111025898682116530836_1417074190743&_=1417074190744".
the chrome debugger network tab indicates correct file downloaded why isn't json being logged console?
the error message indicates response mime type 'text/plain'. expecting script mime type.
so need setup response @ backend dictionary.py
(if under control). add content-type "application/x-javascript"
response header. similar (in java):
@requestmapping(value = "/test/fake") public void testfake(httpservletrequest request, httpservletresponse response) throws ioexception { string callback = request.getparameter("jsonpcallback"); simplejson json = new simplejson("success", 0); jsonobject resultjson = jsonobject.fromobject(json); response.setcontenttype("application/x-javascript"); // set content-type "application/x-javascript" printwriter out = response.getwriter(); if (callback == null) { out.println("{error: 'callback function not defined.'}"); } else out.println(callback + "(" + resultjson.tostring(1, 1) + ")"); }
Comments
Post a Comment