【问题标题】:How respond in HTML and JSON with Rails?如何使用 Rails 响应 HTML 和 JSON?
【发布时间】:2020-09-16 18:10:59
【问题描述】:

我有

(家庭控制器)

    def default

        users = Users.all

        respond_to do |format|

            format.html 
            format.json { render json: users}

        end

     end

(查看)

axios({
        method: 'get',
        url: '/home',
        headers: { 'Accept': 'application/json', 'Content-Type': 'application/json' }
        }).then(response => console.log(response))

带有 json 标头,但我得到了

ActionController::UnknownFormat (LoginController#default is missing a template for this request format and variant.

request.formats: ["application/json"]
request.variant: []):

带有 http 错误 406(不可接受)

感谢您的帮助:)

编辑:

我发现了问题,实际上我的 Rails 将我的请求作为 '/homehome' 我不知道为什么?我在 axios url 参数中将 '/home' 更改为 '' 并且它现在可以工作了,但我仍然不明白发生了什么??

我的 Web 控制台中也有此消息

A cookie associated with a cross-site resource at http://youtube.com/ was set 
without the `SameSite` attribute. A future release of Chrome will only deliver
cookies with cross-site requests if they are set with `SameSite=None` and
`Secure`. You can review cookies in developer tools under
Application>Storage>Cookies and see more details at
https://www.chromestatus.com/feature/5088147346030592 and
https://www.chromestatus.com/feature/5633521622188032.

【问题讨论】:

    标签: javascript html ruby-on-rails ruby


    【解决方案1】:

    你有没有为 axios 尝试过这个配置 responseType: 'json', // default,详情你可以阅读 axios 文档。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-28
      • 1970-01-01
      • 2014-03-25
      • 1970-01-01
      相关资源
      最近更新 更多