解決Spring @ResponseBody註解返回字符串IE提示下載

在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;

@RequestMapping("/url")  
public ResponseEntity<String> doSomething() {  
    HttpHeaders headers = new HttpHeaders();  
    headers.setContentType(MediaType.TEXT_PLAIN);  
    return new ResponseEntity<String>("字符串!", headers, HttpStatus.OK);  
}  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章