没有"Access-Control-Allow-Origin"标头,但无权访问API [英] No 'Access-Control-Allow-Origin' header but no access to API
问题描述
我正在尝试使用UK Parliament API,但遇到此错误:
I'm trying to use the UK Parliament API but I'm hitting this error:
XMLHttpRequest无法加载 http://findyourmp.parliament.uk/api/search?q = london& f = js .所请求的资源上没有"Access-Control-Allow-Origin"标头.因此,不允许访问来源" http://example.com .
XMLHttpRequest cannot load http://findyourmp.parliament.uk/api/search?q=london&f=js. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://example.com' is therefore not allowed access.
我知道这是什么意思,但是没有JSONP选项,而且我无法编辑议会的API.我该如何解决?我可以通过服务器上的PHP文件路由请求,该文件可以允许访问还是不能解决问题?
I know what it means, but there's no JSONP option and I can't edit Parliament's API. How can I get around this? Could I route the request through a PHP file on my server that I can allow access to or would that not solve the problem?
这是我的代码:
var search_term = $('#input').val();
var url = 'http://findyourmp.parliament.uk/api/search?q=' + search_term + '&f=js';
$.getJSON(url, function(jd) {
$('#div').html('<p>Constituency: ' + jd.constituency_name + '</p>');
});
编辑
推荐答案
是的,使用服务器端代理"可以解决此问题,因为Access-Control-Allow_Origin用于客户端调用
yes using serverside "proxy" would solve the problem, as the Access-Control-Allow_Origin is for client-side calls
简单的php代码段
$search_term = "find";
$url = "http://findyourmp.parliament.uk/api/search?q=".$search_term."&f=js";
return file_get_contents($url);
这篇关于没有"Access-Control-Allow-Origin"标头,但无权访问API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!