Rails CORS and jQuery autocomplete

Rails CORS and jQuery autocomplete

本文关键字:autocomplete jQuery and CORS Rails      更新时间:2023-09-26

我有一个基于Rails的API。我正在尝试获取数据并通过 jQuery UI 自动完成在选择中显示它。但是有一个错误:

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.

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

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

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:*

但是使用此代码,服务器不会提供标头:

$(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); }
      });
    }
  });
});

我应该怎么做才能让它工作?

为什么提供指向

的链接

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

但试图提出请求

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

使用第一个链接一切正常:JSFIDDLE