我正在尝试将 json 数据作为 grails 中的响应正文发送。我尝试使用以下方法将 Content-Type 标头设置为 application/json:
render (status: httpServletResponse, text: responseToRender as JSON, contentType: "application/json")
每次生成的 header 如下:
Content-Type: application/json;charset=utf-8
如何摆脱 charset=UTF-8 后缀?
您无法摆脱字符集后缀。
您可以使用此处定义的字符集参数更改它:
https://docs.grails.org/latest/ref/Controllers/render.html https://docs.grails.org/latest/ref/Controllers/render.html
你也可以不提供任何信息,只传递 json 来渲染,例如:
response.setContentType("application/json")
render JsonOutput.toJson(responseToRender);
但是,这将默认为 HTTP 1.1 所需的标准编码,即 ISO-8859-1。因此你的结果将是 application/json;charset=ISO-8859-1
https://www.w3.org/International/articles/http-charset/index.en https://www.w3.org/International/articles/http-charset/index.en
因此,如果您以某种方式需要使用此参数,您可以使用 .split(";")[0] 来仅访问第一部分。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)