rack-cors icon indicating copy to clipboard operation
rack-cors copied to clipboard

CORS headers are being returned for non-CORS OPTIONS requests

Open cyu opened this issue 11 years ago • 1 comments

This OPTIONS request in this comment should not be returning CORS headers: https://github.com/cyu/rack-cors/issues/20#issuecomment-46523416

cyu avatar Oct 14 '14 22:10 cyu

I was able to fix this issue by only returning headers instead of redirecting. This was accomplished in my Rails API by placing the following code in my create action:

def create
@passage = Passage.new(passage_params)

respond_to do |format|
  if @passage.save
    # formatting this way is required for Angularjs app to not return a http 406 status
    format.html do
      head 200, content_type: "text/plain"
    end
    format.json { render json: @passage.to_json }

    # previous code is commented out in the following 2 lines
    # format.html { redirect_to @passage, notice: 'Passage was successfully created.' }
    # format.json { render :show, status: :created, location: @passage }
  else
    format.html { render :new }
    format.json { render json: @passage.errors, status: :unprocessable_entity }
  end
end

end

See my post here for additional details: http://stackoverflow.com/q/26977623/964018

scervera avatar Dec 28 '14 14:12 scervera