如何从响应中传递字符串并获取它的ajax调用

时间:2016-04-24 09:24:31

标签: jquery ajax servlets

我正在尝试从响应中获取字符串文本。 我在其中一个方法中设置了它。 这是对控制器的Ajax调用。

function SaveItems() {

        $.ajax({
                type : "POST",
                url : '/myProject/dispatcherServlet/controller/saveItem',
                data : jsondata,
                dataType : "json",
                contentType: "application/json",
                crossDomain:true,
                success : function(data) {
                    var res=data;
                    alert("data :"+data);
                },
                error : function(data) {

                }
                });
    }

控制器方法

@RequestMapping(value = "/saveItems",headers = "Accept=application/json", method = RequestMethod.POST)
    @ResponseBody
    public String saveItems(@RequestBody final String configJson,HttpServletRequest request, HttpServletResponse response)
    {
        Object json = null;
        ObjectMapper objectMapper = new ObjectMapper();

        try {
            ProfileModel profileModel = objectMapper.readValue(
                    configJson,profileModel.class);
        configServiceImpl.createProfile(profileModel,request ,response);
        } catch (JsonParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (JsonMappingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return "success";

    }

我在Response对象中设置字符串的服务方法。

    public void createProfile(profileModel profileModel,HttpServletRequest request,HttpServletResponse response)
        {

                String resultID ="error01";
                String resultText="Error in creating profile"
                String appnededText =resultID+resultText;
                response.setContentType("text/html");
                PrintWriter pw = response.getWriter();
                pw.write(appnededText);
                pw.close();

}

当我执行代码时,我收到了以下错误

java.lang.IllegalStateException: getWriter() has already been called for this response
    at org.apache.catalina.connector.Response.getOutputStream(Response.java:588)
    at org.apache.catalina.connector.ResponseFacade.getOutputStream(ResponseFacade.java:183)
    at org.springframework.http.server.ServletServerHttpResponse.getBody(ServletServerHttpResponse.java:71)
    at org.springframework.http.converter.StringHttpMessageConverter.writeInternal(StringHttpMessageConverter.java:109)
    at org.springframework.http.converter.StringHttpMessageConverter.writeInternal(StringHttpMessageConverter.java:40)
    at org.springframework.http.converter.AbstractHttpMessageConverter.write(AbstractHttpMessageConverter.java:180)
    at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:143)
    at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:89)
    at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.handleReturnValue(RequestResponseBodyMethodProcessor.java:189)
    at org.springframework.web.method.support.HandlerMethodReturnValueHandlerComposite.handleReturnValue(HandlerMethodReturnValueHandlerComposite.java:69)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:122)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:743)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:672)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:82)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:919)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:851)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:953)
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:855)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)

如何从响应中发送字符串并将其输入“Ajax'

1 个答案:

答案 0 :(得分:1)

您可以根据错误将HttpServletResponse的状态代码设置为500或400,然后将错误消息作为字符串返回。 您正在使用PrintWriter并在createProfile中关闭它的事实可能会导致此问题。

if (index >= '0' && index <= '9')

然后,您应该在成功/错误回调处理程序的数据参数的responseBody属性中获取字符串