简体   繁体   English

为什么 Rails 无法识别请求 header 中的 `Accept: application/json`?

[英]Why does rails not recognize `Accept: application/json` in the request header?

I have some frontend javascript that makes an asynchronous http request to my backend rails server.我有一些前端 javascript 向我的后端 rails 服务器发出异步 http 请求。 On the frontend I am not using XHR (I use axios , although that's not entirely relevant to the question).在前端我没有使用 XHR(我使用axios ,尽管这与问题并不完全相关)。

In the request, I set the following to tell the server I'm sending JSON and to make sure I get JSON back:在请求中,我设置以下内容来告诉服务器我正在发送 JSON 并确保我得到 JSON 回来:

const config = {
  headers: {
    "Content-Type": "application/json",
    "Accept": "application/json"
  }
};

In my backend Rails controller if inspect the request I can verify the Accept header:在我的后端 Rails controller 如果检查请求我可以验证Accept header:

> request.headers
"HTTP_ACCEPT"=>"application/json, text/plain, */*"

However ActionPack/Rails still does not respect that and defaults to the format being :html但是 ActionPack/Rails 仍然不尊重这一点,默认format:html

> request.format
=> #<Mime::Type:0x00007fe223919f80 @hash=-1773238723920954657, @string="text/html", @symbol=:html, @synonyms=["application/xhtml+xml"]>

Why is that?这是为什么?

I know I can append .json to my request URL to "force" it to specify that format, but is that the only option?我知道我可以 append .json对我的请求 URL “强制”指定该格式,但这是唯一的选择吗? I can append it easily but it seems like an implementation specific to Rails and not really the "right" approach.我可以很容易地 append ,但它似乎是特定于 Rails 的实现,而不是真正的“正确”方法。

Additionally, the source code for the request.format method explicitly sets :json as the format on XHR requests - does rails only respect XHR requests at the moment?此外, request.format方法的源代码明确将:json设置为 XHR 请求的格式——rails 目前是否只考虑 XHR 请求?

Thanks!谢谢!

What you are doing is correct, but sending the Axios/Fetch API requests from browser will show "CORS" error from the browser end(you can see it in your browser dev tools console).您所做的是正确的,但是从浏览器发送 Axios/Fetch API 请求将从浏览器端显示“CORS”错误(您可以在浏览器开发工具控制台中看到它)。 You can know more about it from MDN Docs你可以从MDN Docs了解更多

To resolve this, You'll need to send the Access-Control-Allow-Origin Header to the requests you receive on your web server.要解决此问题,您需要将Access-Control-Allow-Origin Header 发送到您在 web 服务器上收到的请求。 You can do it manually by adding this header in application_controller.rb or Simply use a gem like rack-cors .您可以通过在application_controller.rb中添加这个 header 手动完成,或者只需使用像rack-cors这样的 gem。 I'll show you how to do using rack-cors gem:我将向您展示如何使用rack-cors gem:

  • Add gem 'rack-cors' in your Gemfile在您的Gemfile中添加gem 'rack-cors'
  • Run the command bundle i运行命令bundle i
  • In your config/application.rb file, add the following lines:config/application.rb文件中,添加以下行:

     config.middleware.insert_before 0, Rack::Cors do allow do origins '*' resource '*', headers: :any, methods: [:get, :post, :options] end end
  • Now restart your rails server现在重新启动您的 Rails 服务器

  • You can also see above instructions and more details here您还可以在此处查看上述说明和更多详细信息

Now make the API call again without forcing .json at the end of the URL and it should work.现在再次调用 API 而不在 URL 的末尾强制.json并且它应该可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM