【问题标题】:Not getting html response from servelet through ajax没有通过ajax从servlet获得html响应
【发布时间】:2013-09-05 05:09:30
【问题描述】:

我已经制作了ckeditor插件来将doc文件转换为html,文件通过带有ajax的servlet上传并转换为html。但我没有从 servlet 获得所需的 html 响应。我在结果中得到了相同的 jsp 页面。

这里是 Servlet 代码。

        protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException { 
    // HttpSession
            response.setContentType("text/html");                                                                           // session;
            if (!ServletFileUpload.isMultipartContent(request)) {
                response.sendError(HttpServletResponse.SC_FORBIDDEN,
                        "only multipart requests are allowed");
                return;
            }
            WebappContext webappContext = WebappContext.get(getServletContext());
            ServletFileUpload fileUpload = webappContext.getFileUpload();

            OfficeDocumentConverter converter = webappContext
                    .getDocumentConverter();
            String outputExtension = FilenameUtils.getExtension(request
                    .getRequestURI());
            FileItem uploadedFile;
            try {
                uploadedFile = getUploadedFile(fileUpload, request);
                // FileHandler.checkForValidExtn(uploadedFile);
            } catch (FileUploadException fileUploadException) {
                throw new ServletException(fileUploadException);
            }
            if (uploadedFile == null) {
                throw new NullPointerException("uploaded file is null");
            }
            String inputExtension = FilenameUtils.getExtension(uploadedFile
                    .getName());
            String baseName = FilenameUtils.getBaseName(uploadedFile.getName());
            // File fileFolder=new File(getServletContext().getRealPath("/")
            // +"images");
            // fileFolder.mkdir();
            // File inputFile=new File(fileFolder+"\\"+ baseName + "." +
            // inputExtension);
            File inputFile = new File(getServletContext().getRealPath("/")
                    + baseName + "." + inputExtension);

            writeUploadedFile(uploadedFile, inputFile);
            File outputFile = new File(getServletContext().getRealPath("/")
                    + baseName + "." + "html");
            // File outputFile=new File(fileFolder+"\\"+ baseName + "." + "html");
            // outputFile.mkdirs();
            System.out.println("input file path:" + inputFile.getAbsolutePath());
            System.out.println("outPut file path:" + outputFile.getAbsolutePath());
            try {
                // DocumentFormat outputFormat = converter.getFormatRegistry()
                // .getFormatByExtension(outputExtension);
                long startTime = System.currentTimeMillis();
                converter.convert(inputFile, outputFile);
                long conversionTime = System.currentTimeMillis() - startTime;
                logger.info(String.format(
                        "successful conversion: %s [%db] to %s in %dms",
                        inputExtension, inputFile.length(), outputExtension,
                        conversionTime));
            } catch (Exception exception) {
                logger.severe(String.format(
                        "failed conversion: %s [%db] to %s; %s; input file: %s",
                        inputExtension, inputFile.length(), outputExtension,
                        exception, inputFile.getName()));
                throw new ServletException("conversion failed", exception);
            } finally {
                 inputFile.delete();
            }
            PrintWriter out = response.getWriter();
            out.println("<b>File Sent to server</b>");
            out.close();
}

这里是 sendi 文件到服务器的代码 javascript 代码。

CKEDITOR.dialog.add( 'uploadDoc', function( editor ) 
{    var dialougeDefination = 
        {   title:          editor.lang.uploadDoc.labelName,
            resizable:      CKEDITOR.DIALOG_RESIZE_BOTH,
            minWidth:       250,
            minHeight:      50,
            onOk:       function()
                {   if(document.getElementById("flFileBrowser").value == "")
                        {   alert("No file selected");
                            //return;
                        }
                        else
                        {
                            $.ajax({

                                beforeSend: function(){
                                    {    $("#frmFileUpload").ajaxSubmit();
                                    $.blockUI();
                                    }
                                },

                                success: function(result)
                                {   alert(result);

                                    $.unblockUI();
                                }

                            });

【问题讨论】:

  • 所以有很多代码可以调试。不知道问题出在服务器还是客户端。
  • 看起来你用来测试的浏览器不支持通过 ajax 上传文件(更多信息here。最好使用/调整一个插件,比如blueimp jquery file upload plugin
  • 文件正在上传并转换为 html。问题是 servlet 没有在结果字符串中发送响应。我得到的响应与我放置的同一个 jsp 页面 ckeditor。
  • ckeditor 在 iframe 中吗?

标签: java jquery ajax servlets ckeditor


【解决方案1】:

响应实际上是在responseText字段中,所以你的代码应该是

alert(result.responseText);

【讨论】:

  • 我已经写了警报(结果);结果以纯字符串形式出现,没有关联的属性。
  • success: function(result) - 这个结果有一个 responseText 字段。见stackoverflow.com/questions/7598821/…
  • result.responseText 未定义,可能是 beforeSend: function(){ { $("#frmFileUpload").ajaxSubmit(); $.blockUI(); } } 正在创建一个问题。在 beforeSend 回调中我正在上传文件
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-01-31
  • 1970-01-01
  • 2021-01-06
  • 2014-01-30
  • 1970-01-01
  • 1970-01-01
  • 2011-03-11
相关资源
最近更新 更多