I have an HTML page with a JavaScript code that sends AJAX request. The request is being handled by my servlet. I've thought that it's "better" and "more correct" to return the data as "application/json" MIME type from the servlet (response content type). However, it drives MSIE crazy - meaning the browser seems not to be capable to display/process this MIME type (Chrome/FF are just fine). When I specify no type explicitly - it works fine in all browsers. Is it real that no MIME type should be returned from servlet for AJAX requests?
Update: my server side is implemented in Java and the MIME type is being defined by a following line:
response.setContentType("application/json");
the response is the following text (only an example):
{ "status" : "DONE", "progress" : 100, "url" : "/7909118672283641787.docx" , "totalBytes" : 17696 }
Update2: the snippet made of my client-side 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);
}
}
Return it as raw text then use JSON.parse() to convert to JSON that is useable.
JSON is not text, so Firefox doesn't know WHAT to do with it. Therefore, you need to basically serve it as something it does know.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With