<small id='6lopz'></small><noframes id='6lopz'>

    <legend id='6lopz'><style id='6lopz'><dir id='6lopz'><q id='6lopz'></q></dir></style></legend>

    <tfoot id='6lopz'></tfoot>
      • <bdo id='6lopz'></bdo><ul id='6lopz'></ul>
      <i id='6lopz'><tr id='6lopz'><dt id='6lopz'><q id='6lopz'><span id='6lopz'><b id='6lopz'><form id='6lopz'><ins id='6lopz'></ins><ul id='6lopz'></ul><sub id='6lopz'></sub></form><legend id='6lopz'></legend><bdo id='6lopz'><pre id='6lopz'><center id='6lopz'></center></pre></bdo></b><th id='6lopz'></th></span></q></dt></tr></i><div id='6lopz'><tfoot id='6lopz'></tfoot><dl id='6lopz'><fieldset id='6lopz'></fieldset></dl></div>

    1. 如何在 Rails 4 应用程序中启用 CORS

      时间:2023-10-14
        <bdo id='miuo0'></bdo><ul id='miuo0'></ul>

        <small id='miuo0'></small><noframes id='miuo0'>

            <legend id='miuo0'><style id='miuo0'><dir id='miuo0'><q id='miuo0'></q></dir></style></legend>
          1. <tfoot id='miuo0'></tfoot>

                  <tbody id='miuo0'></tbody>
              • <i id='miuo0'><tr id='miuo0'><dt id='miuo0'><q id='miuo0'><span id='miuo0'><b id='miuo0'><form id='miuo0'><ins id='miuo0'></ins><ul id='miuo0'></ul><sub id='miuo0'></sub></form><legend id='miuo0'></legend><bdo id='miuo0'><pre id='miuo0'><center id='miuo0'></center></pre></bdo></b><th id='miuo0'></th></span></q></dt></tr></i><div id='miuo0'><tfoot id='miuo0'></tfoot><dl id='miuo0'><fieldset id='miuo0'></fieldset></dl></div>
                本文介绍了如何在 Rails 4 应用程序中启用 CORS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我正要拔掉头发...从早上开始,我一直在尝试在这个 Rails 应用程序中启用 CORS,但它不起作用.我试过 this,使用 Rack Cors Gem, 这个答案和这个post都没有成功.

                I'm just about to pull my hair out... I've been trying to enable CORS in this Rails app since the morning and it just doesn't work. I've tried this, using Rack Cors Gem, this answer and this post all without success.

                有人能指出正确的方向吗?

                Can someone point me in the right direction?

                这是我的 js:

                      var req = new XMLHttpRequest();
                
                      if ('withCredentials' in req) {
                            // req.open('GET', "https://api.github.com/users/mralexgray/repos", true);
                            req.open('GET', "http://www.postcoder.lc/postcodes/" + value, true);
                            // Just like regular ol' XHR
                            req.onreadystatechange = function() {
                                if (req.readyState === 4) {
                                    if (req.status >= 200 && req.status < 400) {
                                        // JSON.parse(req.responseText) etc.
                                        console.log(req.responseText);
                                    } else {
                                        // Handle error case
                                    }
                                }
                            };
                            req.send();
                        }
                

                当我尝试这个网址时(来自外部客户端):https://api.github.com/users/mralexgray/repos 工作正常,我假设问题出在我的 Rails API 上.我错了吗?

                When I try this url (from an external client): https://api.github.com/users/mralexgray/repos that works ok, I'm assuming the problem is with my Rails API. Am I wrong?

                目前我的控制器中有这个:

                skip_before_filter :verify_authenticity_token
                before_filter :cors_preflight_check
                after_filter :cors_set_access_control_headers
                
                # For all responses in this controller, return the CORS access control headers.
                def cors_set_access_control_headers
                  headers['Access-Control-Allow-Origin'] = '*'
                  headers['Access-Control-Allow-Methods'] = 'POST, GET, OPTIONS'
                  headers['Access-Control-Max-Age'] = "1728000"
                end
                
                # If this is a preflight OPTIONS request, then short-circuit the
                # request, return only the necessary headers and return an empty
                # text/plain.
                
                def cors_preflight_check
                  headers['Access-Control-Allow-Origin'] = '*'
                  headers['Access-Control-Allow-Methods'] = 'POST, GET, OPTIONS'
                  headers['Access-Control-Allow-Headers'] = 'X-Requested-With, X-Prototype-Version'
                  headers['Access-Control-Max-Age'] = '1728000'
                end
                

                推荐答案

                你应该使用 rack cors

                它提供了一个很好的 DSL,可以在你的 config/application.rb 中使用,而不是在过滤器之前使用凌乱的标题.

                It provides a nice DSL, to use in your config/application.rb, instead of the messy header work and before filters.

                一个非常宽松的方法如下,但当然,你必须稍微调整一下.

                A very permissive would be as follows, but of course, you'll have to tailor it a bit.

                use Rack::Cors do
                  allow do
                    origins '*'
                    resource '*', headers: :any, methods: :any
                  end  
                end
                

                这篇关于如何在 Rails 4 应用程序中启用 CORS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:jQuery AJAX 调用导致错误状态 403 下一篇:XmlHttpRequest 状态 0 而不是 IE 10 中的 401

                相关文章

                最新文章

              • <tfoot id='324sp'></tfoot>

                    <small id='324sp'></small><noframes id='324sp'>

                    <i id='324sp'><tr id='324sp'><dt id='324sp'><q id='324sp'><span id='324sp'><b id='324sp'><form id='324sp'><ins id='324sp'></ins><ul id='324sp'></ul><sub id='324sp'></sub></form><legend id='324sp'></legend><bdo id='324sp'><pre id='324sp'><center id='324sp'></center></pre></bdo></b><th id='324sp'></th></span></q></dt></tr></i><div id='324sp'><tfoot id='324sp'></tfoot><dl id='324sp'><fieldset id='324sp'></fieldset></dl></div>
                      <bdo id='324sp'></bdo><ul id='324sp'></ul>
                  1. <legend id='324sp'><style id='324sp'><dir id='324sp'><q id='324sp'></q></dir></style></legend>