2017-07-19 161 views
0

我試圖發送json數據作爲grails中的響應主體。我試着用以下方法中的Content-Type頭設置爲application/JSON:如何刪除內容類型標題中的charset = utf-8,由grails生成

render (status: httpServletResponse, text: responseToRender as JSON, contentType: "application/json") 

每次生成的頭如下:

Content-Type: application/json;charset=utf-8 

如何擺脫字符集= UTF-8後綴?

回答

1

你不能擺脫charset後綴。

response.setContentType("application/json") 
render JsonOutput.toJson(responseToRender); 

然而,這將默認爲:

https://docs.grails.org/latest/ref/Controllers/render.html

您也可以通過只交給JSON渲染,如不提供信息: 你可以在這裏定義的字符集參數更改HTTP 1.1所要求的標準編碼是ISO-8859-1。因此,你的結果將是應用程序/ JSON的;字符集= ISO-8859-1

https://www.w3.org/International/articles/http-charset/index.en

所以,如果你以某種方式需要使用這個參數,你可以使用.split( 「;」)[0]到只訪問第一部分。

相關問題