当前位置: 动力学知识库 > 问答 > 编程问答 >

ruby on rails - Cross-Origin Request Blocked with Ajax after taking site to SSL

问题描述:

We are using Ruby 2.1.2 with Rails 3.2.14.

We are getting the following error on browser's console for ajax requests after we moved our site to SSL.

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://sitename.com/xxx/xx?id=xx. This can be fixed by moving the resource to the same domain or enabling CORS.

We've tried adding

headers: { 'Access-Control-Allow-Origin': '*' },

crossDomain: true

but no effect. Here is the ajax code :

$.ajax({

type: "GET",

data: {id: id},

url: path+id,

headers: { 'Access-Control-Allow-Origin': '*' },

crossDomain: true,

success: function(data) { }

});

Any other suggestions??

网友答案:

You have to set headers on server side. Not on the client. The simplest way will be use something like this in your ApplicationController:

after_filter  :set_access_control_headers

def set_access_control_headers
  headers['Access-Control-Allow-Origin'] = '*'
end   
分享给朋友:
您可能感兴趣的文章:
随机阅读: