使用标头呈现JSON [英] Render JSON with header

查看:120
本文介绍了使用标头呈现JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在控制器中使用以下 cors 标头呈现JSON:

I want to render JSON with the following cors header in my controller:

'Access-Control-Allow-Origin' = '*'.

我试过这个:

def my_action
  render(json: some_params)
  response.headers['Access-Control-Allow-Origin'] = '*'
end

但我有一个 AbstractController :: DoubleRenderError 。有没有方法用头来渲染JSON?

but I got an AbstractController::DoubleRenderError. Are there ways to render JSON with headers?

推荐答案

渲染后不能设置标题,因为响应是发送的。

You can't set a header after render, because the response is sent. So change the headers after makes no sense and you get this error.

尝试:

def my_action
  response.headers['Access-Control-Allow-Origin'] = '*'
  render(json: some_params)
end

这篇关于使用标头呈现JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆