json - ExtJS 4 Spring 3 文件上传。服务器发送错误响应内容类型

标签 json spring extjs upload response

我正在使用 ExtJS 4 前端和 Spring 3 作为后端创建文件上传。文件上传有效,但来自服务器的响应内容类型错误。当我发送 {success:true}使用 Map<String, Object>由 Jackson 序列化,ExtJS 返回错误

Uncaught Ext.Error: You're trying to decode an invalid JSON String: <pre style="word-wrap: break-word; white-space: pre-wrap;">{"success":true}</pre>

为什么我的回复是 <pre>标签?我已经搜索和 found out我应该将响应类型更改为 text/html例如。但是changing content type in servlet response没有帮助
@RequestMapping(value = "/upload", method = RequestMethod.POST)
public @ResponseBody Map<String, Object> upload(
    FileUpload uploadItem, BindingResult result, HttpServletResponse response) {

    response.setContentType("text/html");

    // File processing   

    Map<String, Object> jsonResult = new HashMap<String, Object>();
    jsonResult.put("success", Boolean.TRUE);
    return jsonResult;
}

当我更改 upload 的返回值时方法 String ,一切正常,但我想返回 Map并由 jackson 连载
@RequestMapping(value = "/upload", method = RequestMethod.POST)
public @ResponseBody String upload(
    FileUpload uploadItem, BindingResult result, HttpServletResponse response) {

    // File processing   

    return "{success:true}";
}

我的 Spring 配置
<bean 
    id="stringHttpMessageConverter" 
    class="org.springframework.http.converter.StringHttpMessageConverter">
</bean>
<bean 
    id="jacksonMessageConverter" 
    class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/>  
<bean    
    class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    <property name="messageConverters">
        <list>
            <ref bean="jacksonMessageConverter"/>
            <ref bean="stringHttpMessageConverter" />
        </list>
    </property>
</bean>

如何告诉 Spring 返回正确的内容类型? 当其他方法的响应被正确解释时,为什么该方法的响应不正确?

最佳答案

您需要将响应的内容类型设置为“text/html”。
如果内容类型是“application/json”就会有这个问题。这很奇怪。

关于json - ExtJS 4 Spring 3 文件上传。服务器发送错误响应内容类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7023531/

相关文章:

php - 服务器将数据发送到表中

javascript - 在 json 中存储函数 [Javascript/Coffeescript]

html - 在 ExtJS 中使用 CSS !important setFieldStyle()

Extjs - 使用文件字段上传文件

javascript - AngularJS:将对象存储在 cookie 中,给出 [Object Object] 的结果

javascript - 从数组中删除引号以充当函数(变量)

java - 使用 Struts2 和 hibernate 创建 Web 服务(SOAP 或 REST)

java - Spring:将@Qualifer 附加到 Java 配置的 bean

java - com.ibm.wsspi.uow.UOWManager 的 NoClassDefFoundError

jquery - 在 ajax 请求中重新加载数据