I have an HTML page with JavaScript code that sends an AJAX request. The request is being processed by my servlet. I thought it was “better” and “more correct” to return the data as an “application / json” MIME type from a servlet (response content type). However, this leads MSIE to be crazy - this means that the browser does not seem to be able to display / process this MIME type (Chrome / FF is fine). When I don't specify the type explicitly, it works fine in all browsers. Is it really that the MIME type should not be returned from the servlet for AJAX requests?
Update: my server side is implemented in Java, and the MIME type is determined by the following line:
response.setContentType("application/json");
The answer is the following text (just an example):
{ "status" : "DONE", "progress" : 100, "url" : "/7909118672283641787.docx" , "totalBytes" : 17696 }
Update2: snippet made from my client code (plain javascript, no libraries)
function display_progress(http) {
if (http.readyState == 4) {
var again = false;
if (http.status != 200) {
document.getElementById('progress_bar').innerHTML = "Wrong response status received: " + http.status + "! Fix the server-side code.";
} else {
try {
var resp = eval('(' + http.responseText + ')');
var status = resp['status'];
if (status == 'DOING') {
document.getElementById('progress_bar').innerHTML = "Uploaded: " + resp['progress'] + "%";
again = true;
} else if (status == 'DONE'){
document.getElementById('progress_bar').innerHTML =
"Uploaded 100% (" + resp['totalBytes'] + " bytes)! Your file is <a href=\"" + resp['url'] + "\"/>" + "here" + "</a>";
} else if (status == 'ERROR') {
document.getElementById('progress_bar').innerHTML = "Error while uploading!";
} else {
document.getElementById('progress_bar').innerHTML = "Unexpected state: " + status + "! Fix the server-side code.";
}
} catch (ex) {
document.getElementById('progress_bar').innerHTML = "Wrong response received: " + resp + "! Fix the server-side code.";
}
}
if (again) {
setTimeout("update_progress()", 500);
}
}
source
share