I've written an angular app that is part of a rails app. The idea is that a third party web site can create a script tag in their page pointing to my rails method where they want my app to show up.
In my rails controller I have logic that returns raw javascript like below...
def job_board
div_init_script =
"var jobBoardDiv=document.getElementById('#{div_id}');" \
"jobBoardNgView=document.createElement('ng-view');" \
'jobBoardDiv.appendChild(jobBoardNgView);' \
"jobBoardDiv.setAttribute('ng-app','jobBoard');"
url = Rails.application.config.api_url
contents = Rails.cache.fetch 'api_js' do
file_contents = File.read("#{Rails.root}/public/app/app.js")
"(function(){ var hookApiUrl = { apiUrl: '#{url}'};#{file_contents}})();"
end
send_data div_init_script + contents, type: 'text/javascript', disposition: 'inline'
end
So, to recap, my site returns raw javascript executing within another site and my javascript gets/posts to my site's api endpoints.
I came across this gist to get CORS working - https://gist.github.com/dhoelzgen/cd7126b8652229d32eb4 - and when I run it all locally within the rails server, all of the headers are being added to every request.
However CORS won't work when I set up the scenerio of running a page on my localhost that has a script src set to my site's job_board endpoint. From what I've read, angular adds a preflight check to the $http.get and post methods which the preflight check portion of the code from the gist linked above "should" handle. But when I look at the console when running my app, I don't see any preflight check on the calls that are failing. Just a single call with no access-control headers added. I'm not sure where else to go at this point. Can anyone make any suggestions? Thanks.