简体   繁体   English

从Java Servlet方法doGet返回数据到ajax

[英]Return data to ajax from java Servlet method doGet

I have my function ajax like this : 我有这样的功能ajax:

$.ajax({
        url: "associer_type_flux", // It's  my servlet
        dataType : "xml",
        type : "GET",
        data : { },
        success: function(response){
            alert("fine");
        },
        error:  function(data, status, er){
            alert(data+"_"+status+"_"+er);
        }
    });

And my method doGet in my servlet like this : 我的方法doGet在我的servlet中是这样的:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String forward = ""; 
    try {           
        String fluxXML = "";
        ServicesCodeTypeFluxGlobaux servicesCodeTypeFluxGlobaux = new ServicesCodeTypeFluxGlobauxImpl();

            fluxXML = "<lescodetypeflux>";

            fluxXML += "</lescodetypeflux>";
            PrintWriter printWriter  = response.getWriter();
            printWriter.println(fluxXML);

        }
        forward = "/associerCode/accueil_association.jsp";
        getServletContext().getRequestDispatcher(forward).forward(request, response);           
    }
    catch (Exception e) {
        forward = "/erreur.jsp";
        request.setAttribute("msg", e.getMessage());
    }       
}

So my problem is that, I cant get data in my jsp .. But i don't know how get this data or return this data from doGet method .. Now I have an alert Error .. 所以我的问题是,我无法在jsp中获取数据..但是我不知道如何从doGet方法获取此数据或返回此数据..现在我有一个警报Error ..

Thx 谢谢

A few suggestion to make it work - 一些使其可行的建议-

  1. don't forget to set mime type in your case "text/xml" or "application/xml" 不要忘记在您的情况下为“ text / xml”或“ application / xml”设置mime类型

  2. You can not use both together out.println() and requestdispatcher as it will throw exception. 您不能同时使用out.println()和requestdispatcher,因为它将引发异常。 out.println() will print the value in response body but request dispatcher will redirect you to some other page and incase of ajax what you will get is the whole content of the page where you redirected. out.println()将在响应正文中打印该值,但请求分派器会将您重定向到其他页面,如果使用ajax,您将获得的是重定向页面的整个内容。

so in your case you should only use out.println() 所以在你的情况下,你应该只使用out.println()

So your final code should look like this - 因此您的最终代码应如下所示-

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setContentType("application/xml");
    String forward = ""; 
    try {           
        String fluxXML = "";
        ServicesCodeTypeFluxGlobaux servicesCodeTypeFluxGlobaux = new ServicesCodeTypeFluxGlobauxImpl();

            fluxXML = "<lescodetypeflux>";

            fluxXML += "</lescodetypeflux>";
            PrintWriter printWriter  = response.getWriter();
            printWriter.println(fluxXML);
            printWriter.close();
        }        
    }
    catch (Exception e) {
        //print xml with error value
    }       
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM