Sebastian
Sebastian

Reputation: 3626

No 'Access-Control-Allow-Origin' header but no access to API

I'm trying to use the UK Parliament API but I'm hitting this error:

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.

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?

Here's my code:

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>');
});

EDIT

API here.

Upvotes: 0

Views: 236

Answers (1)

john Smith
john Smith

Reputation: 17926

yes using serverside "proxy" would solve the problem, as the Access-Control-Allow_Origin is for client-side calls

simple php snippet

$search_term = "find";
$url = "http://findyourmp.parliament.uk/api/search?q=".$search_term."&f=js";
return file_get_contents($url);

Upvotes: 1

Related Questions