Rails CORS 和 jQuery 自动完成

Rails CORS and jQuery autocomplete

我有点Rails基础API。我正在尝试获取数据并通过 jQuery UI 自动完成将其显示在 select 中。但是出现错误:

XMLHttpRequest cannot load http://api.exline.systems/regions/origin?title=%D0%95%D1%81. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access. The response had HTTP status code 404.

我已经用 headers 完成了所有工作。例如,如果我通过浏览器直接请求: http://api.exline.systems/public/v1/regions/origin.json?title=%D0%B0%D0%BB

我可以看到所有 headers 都由服务器提供服务:

Access-Control-Allow-Headers:Origin, X-Requested-With, Content-Type, Accept, Authorization
Access-Control-Allow-Methods:POST, PUT, DELETE, GET, OPTIONS
Access-Control-Allow-Origin:*
Access-Control-Request-Method:*

但使用此代码,headers 未由服务器提供服务:

$(function() {
  $( '#calc-origin-ajax' ).autocomplete({
    source: function (request, response) {
      $.ajax(
      {
        url: 'http://api.exline.systems/regions/origin',
        dataType: "json",
        data: { title: request.term },
        success: function (data) { response(data); }
      });
    }
  });
});

我应该怎么做才能让它发挥作用?

为什么要提供 link 给

http://api.exline.systems/public/v1/regions/origin.json

但正在尝试向

提出请求

http://api.exline.systems/regions/origin

第一个 link 一切正常:JSFIDDLE