【发布时间】: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