Which MIME type to return?

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.


No this is not true....you can set MIME types for AJAX requests...in case you are not setting them...they take the default value....which happens to be...."text/plain". So I guess the default is working fine for you....A more common response type happens to be "text/html". It all depends on the code that you will be using to handle the response from the servlet...the problem may lie in that part of the code....


如此处所述:http: text/javascript是正确的解决方法。

链接地址: http://www.djcxy.com/p/8258.html

上一篇: .rar和.tar文件缺少MIME类型

下一篇: 哪种MIME类型返回?