开发者

Which MIME type to return?

开发者 https://www.devze.com 2023-03-04 06:15 出处:网络
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 \"applica

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....


As explained here: http://www.entwicklungsgedanken.de/2008/06/06/problems-with-internet-explorer-and-applicationjson/ text/javascript is the correct workaround.

0

精彩评论

暂无评论...
验证码 换一张
取 消