I'm trying to do a simple GET request with an acronym finder API but for some reason the JSON is not being returned. Here is the code:
$.ajax('http://www.nactem.ac.uk/software/acromine/dictionary.py?sf=DOD', {
crossDomain:true,
dataType: "jsonp",
success: function(data){
console.log(data);
}
});
If I visit the url directly in my browser, I can easily see the requested JSON, but my Chrome console only returns:
Resource interpreted as Script but transferred with 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 that the correct file was downloaded but why isn't the JSON being logged to the console?
Try using this
$.ajax('http://www.nactem.ac.uk/software/acromine/dictionary.py?sf=DOD', {
crossDomain:true,
dataType: "json",
contentType: "application/json",
success: function(data){
console.log(data);
}
});
The error message indicates that the response MIME type is 'text/plain'. But it is expecting a Script MIME type.
So you need to setup your response at backend dictionary.py
(if it is under your control). Add content-type "application/x-javascript"
to the response header. Something similar to (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 to "application/x-javascript"
PrintWriter out = response.getWriter();
if (callback == null) {
out.println("{error: 'Callback function is not defined.'}");
} else
out.println(callback + "(" + resultJSON.toString(1, 1) + ")");
}