在Spring中对于Ajax请求 在控制器中可以标注@ResponseBody注解,来让Spring不进行视图渲染 而直接返回字符串。但是IE中总是提示
下载
。
可以用以下方法解决:
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
@RequestMapping("/url")
public ResponseEntity<String> doSomething() {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.TEXT_PLAIN);
return new ResponseEntity<String>("字符串!", headers, HttpStatus.OK);
}
本文介绍了一种解决Internet Explorer浏览器将Ajax响应视为文件下载而非直接显示的方法。通过使用Spring框架提供的`ResponseEntity`与`HttpHeaders`,设置正确的响应类型为文本,确保了Ajax请求的数据能在IE浏览器中正确显示。
18万+

被折叠的 条评论
为什么被折叠?



