Display content of another website in my page using jquery without using iframe in Rails
I have created a web page on my site that will be embedded in another site using jquery. I have to provide the jquery code since when we click the url from another site that has my web page embedded my content is loaded on page load. I cannot use iframe. I used .html, .load, .ajax but they always fail
XMLHttpRequest cannot load http://mypage.com/index?user_id=82 . The requested resource does not have an "Access-Control-Allow-Origin" header. Therefore the original localserver: port 'has no access.
Is there a problem with mypage settings
I checked them:
$('#siteloader').load('http://mypage.com/index?user_id=82');
$("#siteloader").html('<object data="http://mypage.com/index?user_id=82">');
Please suggest
source to share
Thanks guys @Rory McCrossan @Mohammed ElSayed This is how I enabled CORS settings in Rails 4 Inside my app_controller
after_filter :cors_set_access_control_headers
def cors_set_access_control_headers
headers['Access-Control-Allow-Origin'] = '*'
headers['Access-Control-Allow-Methods'] = 'POST, PUT, DELETE, GET, OPTIONS'
headers['Access-Control-Request-Method'] = '*'
headers['Access-Control-Allow-Headers'] = 'Origin, X-Requested-With, Content-Type, Accept, Authorization'
end
source to share
the error message is not actually an error, it is by design so no one can upload other content without proper permission.
you will need to allow CORS requests for the website you are calling.
for IIS7:
consider the following snippet (web.config)
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
</customHeaders>
</httpProtocol>
</system.webServer>
Apache server (.htaccess):
Header set Access-Control-Allow-Origin "*"
Checkout this link for more information: http://enable-cors.org/server.html
source to share