上傳File時,瀏覽器總是添加的解決辦法

來源:互聯網
上載者:User

       在使用Structs的FileUpload組件進行上傳檔案的時候,在返回的JSon字串裡面總是莫名其妙的添加了<pre>標籤,例如返回內容為"{\"message\":\"匯入檔案已成功\",\"result\":\"OK\"}",在瀏覽器裡面就變成了"<pre style="word-wrap: break-word; white-space: pre-wrap;">"{\"message\":\"匯入檔案已成功\",\"result\":\"OK\"}"</pre>",這樣就導致Ajax解析傳回值的時候出現錯誤。

         我的上傳代碼,預設總是進入到error的處理中。

         

 jQuery.ajaxFileUpload({            type:"POST",            url:"/****/upload.action?file="+fileName,            secureuri:false,            fileElementId:"upload",            dataType: "json",            success:function (data, status) {                if (data != null) {                    data = JSON.parse(data);                    MBJ.alert("提示", data.message);                } else {                    MBJ.alert("提示", "上傳檔案出錯, 伺服器返回錯誤資訊: status = " + status);                }            },            error:function (data, status, e) {                MBJ.alert("提示", "上傳檔案出錯: status = " + status);            }        });

      Google了一圈之後,發現很多人和我是一樣的,解決辦法都是把response的傳回型別設定為【text/html】。在Struts2中需要在Action的配置中這樣設定。

  

<result name="success" type="json">                <param name="root">msg</param>                <param name="contentType">text/html;charset=UTF-8</param>            </result>

     如果用的是annotation的方式的話,需要加上這句話

 @Action(value = "upload", results = { @Result(name = SUCCESS, params = { "root", "msg", "contentType",            "text/html;charset=UTF-8" }, type = "json") })

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.