如何删除内容类型标题中的charset = utf-8,由grails生成
问题描述:
我试图发送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后缀?
答
你不能摆脱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]到只访问第一部分。