Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is from Spine js documentation</p> <p>CORs Rails integration</p> <p>Let's create a cor method, which will add some of the request access control headers to the request's response.</p> <p>Add the following to app/application_controller.rb:</p> <pre><code>before_filter :cor def cor headers["Access-Control-Allow-Origin"] = "js-app-origin.com" headers["Access-Control-Allow-Methods"] = %w{GET POST PUT DELETE}.join(",") headers["Access-Control-Allow-Headers"] = %w{Origin Accept Content-Type X-Requested-With X-CSRF-Token}.join(",") head(:ok) if request.request_method == "OPTIONS" end </code></pre> <p>Although Access-Control-Allow-Origin takes a wildcard, I highly recommend not using it as it opens up your app to all sorts of CSRF attacks. Using a whitelist is much better and more secure.</p> <p>The Access-Control-Allow-Headers section is important, especially the X-Requested-With header. Rails doesn't like it if you send Ajax requests to it without this header, and ignores the request's Accept header, returning HTML when it should in fact return JSON.</p> <p>It's worth noting that jQuery doesn't add this header to cross domain requests by default. This is an issue that Spine solves internally, but if you're using plain jQuery for CORs, you'll need to specify the header manually.</p> <pre><code>jQuery.ajaxSetup({ headers: {"X-Requested-With": "XMLHttpRequest"} }); </code></pre> <p>Some browsers send an options request to the server first, to make sure the correct access headers are set. You'll need to catch this in Rails, returning a 200 status with the correct headers. To do this, add the following to your application's config/routes.rb file:</p> <pre><code>match '*all' =&gt; 'application#cor', :constraints =&gt; {:method =&gt; 'OPTIONS'} </code></pre> <p>That's it, you're all set up for Cross Origin Requests with Spine!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload