【问题标题】:Rails CORS and jQuery autocompleteRails CORS 和 jQuery 自动完成
【发布时间】:2015-02-03 15:02:33
【问题描述】:

我有一些基于 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); }
      });
    }
  });
});

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

【问题讨论】:

    标签: javascript jquery ruby-on-rails jquery-ui


    【解决方案1】:

    为什么要提供链接

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

    但试图向

    提出请求

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

    第一个链接一切正常:JSFIDDLE

    【讨论】:

    猜你喜欢
    • 2012-03-20
    • 1970-01-01
    • 2014-04-02
    • 2016-02-24
    • 2011-02-26
    • 2011-04-26
    • 1970-01-01
    • 2011-07-01
    • 2010-09-23
    相关资源
    最近更新 更多