![](/img/trans.png)
[英]How to change Content-Type:application/json into html in response header
[英]Response header does not return Content-Type application/json
我正在嘗試將Response標頭的Content-Type更改為'application / json。 嘗試了多種方式,但是curl仍然包含“文本/純文本”。
def my_action
file = File.read("#{Rails.root}/public/my_json")
json = JSON.parse(file)
# response.set_header('Content-Type', 'application/json')
# response.headers['Content-Type'] = 'application/json'
# render :text => json, :content_type => 'application/json'
# render :json => json, :content_type => 'application/json'
# render json: json, content_type: 'application/json'
render text: json, content_type: 'application/json'
end
卷曲響應為
HTTP/1.1 200 OK Last-Modified: Thu, 09 Nov 2017 14:39:01 GMT Content-Type: text/plain Content-Length: 181 Server: WEBrick/1.3.1 (Ruby/2.2.2/2015-04-13) Date: Thu, 09 Nov 2017 15:04:45 GMT Connection: Keep-Alive
Rails 5.0.6 Ruby 2.2.2
有任何想法嗎?
找出問題。 該路由與靜態文件具有相同的名稱,並且靜態文件“ my_json”的優先級高於路由“ my_json”。
get 'my_json', to: 'files#my_action'
結果導致響應標頭為text/plain
而不是application/json
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.