我知道這個問題已被詢問很多次,但在我的情況。 Firefox是工作,但Chrome是給我這個錯誤:拒絕從'URL'執行腳本,因爲它的MIME類型('application/json')是不可執行的,並且啓用嚴格的MIME類型檢查
Refused to execute script from 'http://localhost:3000/get_all_test_centers?callback=undefined&_=1409807050144' because its MIME type ('application/json') is not executable, and strict MIME type checking is enabled.
我從那裏我送一個REST調用軌服務器(JSONP請求)WordPress的網站。我下面的變化作出了CORS在Rails
mime_types.rb
Mime::Type.register 'application/json', :js
application_controller.rb
before_filter :set_access_control_headers
def set_access_control_headers
headers['Access-Control-Allow-Origin'] = Rails.application.secrets.website_url
headers['Access-Control-Request-Method'] = 'GET, OPTIONS, HEAD'
headers['Access-Control-Allow-Headers'] = 'x-requested-with,Content-Type, Authorization'
end
Rails.application.secrets.website_url
值http://localhost/
控制研究呃輸出:
def get_all_test_centers
test_centers = TestCenter.all
respond_to do |format|
format.js do
render :json => test_centers, :callback => 'renderTestCenters'
end
end
end
我的WordPress JS:
var renderTestCenters = function(data) {
console.log(data);
};
$.ajax({
url: "http://localhost:3000/get_all_test_centers",
crossDomain: true,
type: "GET",
dataType: "JSONP",
jsonpCallback: renderTestCenters
});
它工作正常,在Firefox,但在Chrome它給我的錯誤。