2016-04-24 22 views
0

我想从响应中获取字符串文本。 我把它设置在我的一个方法中。 这里是对控制器的Ajax调用。如何从响应传递字符串,并让它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"; 

    } 

服务方法从我在哪里响应对象设定字符串。

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) 

如何从响应发送字符串,并把它在“阿贾克斯”

回答

1

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

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

    //perform operation 
    return performOperation(); 
} 


@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(); 
    boolean opStatus = false; 
    try { 
     ProfileModel profileModel = objectMapper.readValue(
       configJson,profileModel.class); 
     opStatus = 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(); 
    } 
    if (!opStatus){ 
     response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); 
     String resultID ="error01"; 
     String resultText="Error in creating profile"; 
     String appnededText =resultID+resultText; 
     return appnededText; 
    }else{ 
     return "success"; 
    } 
} 

那么你应该在成功/错误回调处理

+0

的数据参数的responseBody属性中的字符串现在我能够从控制器发送字符串。但我无法使用jsp中的数据。我在响应中使用fire bug看到了响应文本,但是当我提醒我看到Object对象 – Varun

+0

时,数据对象(这是成功的参数,错误回调)由各种属性组成:它们中很少有status,responseText。您可以利用这些获取所需的HTTP响应状态代码和您要发回的字符串 – MohamedSanaulla

相关问题