【发布时间】:2018-06-20 22:01:28
【问题描述】:
我正在尝试使用 jquery 发送一个 post 方法
jQuery.ajax({
headers: { 'Authorization': "Token token=123546" },
url: "https://paycar-public-api.herokuapp.com/api/v1/users",
type: "POST",
processData: false,
contentType: 'application/json',
data:
JSON.stringify({
email: "mikew75+123@gmail.com",
})
});
我明白了
无法加载https://paycar-public-api.herokuapp.com/api/v1/users: 对预检请求的响应未通过访问控制检查:否 请求中存在“Access-Control-Allow-Origin”标头 资源。因此不允许使用原点“https://app.instapage.com” 使用权。响应的 HTTP 状态代码为 500。
我的application.rb 看起来像这样:
class Application < Rails::Application
# Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded.
# Only loads a smaller set of middleware suitable for API only apps.
# Middleware like session, flash, cookies can be added back manually.
# Skip views, helpers and assets when generating a new resource.
config.api_only = true
# CORS
config.middleware.insert_before 0, "Rack::Cors" do
allow do
origins '*'
resource '*', :headers => :any, :methods => [:get, :post, :options]
end
end
end
API 由 heroku 托管
我不确定我是否理解它为什么不起作用,任何帮助将不胜感激。
【问题讨论】:
-
响应的 HTTP 状态码为 500——这就是你需要解决的问题。与您的 CORS 配置无关,正在发生一些内部服务器错误,您需要首先弄清楚并修复它。检查您的服务器日志。您在该浏览器消息中看到提及 Access-Control-Allow-Origin 的唯一原因是您在应用程序代码中设置的标头不会添加到 5xx 响应中。
标签: jquery ruby-on-rails heroku