What type of MIME should return?

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);
        }
    }
+3
source share
3 answers
0
source

Return it as the source text, then use it JSON.parse()to convert to JSON, which you can use.

JSON is not text, so Firefox does not know what to do with it. Therefore, you need to basically serve him as what he knows.

+3
source

No, this is not so .... you can set the MIME types for AJAX requests ... in case you don't set them ... they take the default value .... what happens .... "text / usual". So, I think that the default works fine for you .... The more common type of response is text / html. It all depends on the code that you will use to process the response from the servlet ... the problem may lie in this part of the code ....

0
source

All Articles