24

I'm trying to get access to education.com API data. However, I keep receiving an error the error states:

XMLHttpRequest cannot load http://api.education.com/service/service.php?f=schoolSearch&key=mykey&sn=sf&v=4&city=Atlanta&state=ga&Resf=json. Origin is not allowed by Access-Control-Allow-Origin.

My code is the following:

$(function(){
    $.getJSON('http://api.education.com/service/service.php?f=schoolSearch&key=mykey&sn=sf&v=4&city=Atlanta&state=ga&Resf=json', 
    function(data) {
        console.log(data);
    });
});

Can someone help me please?

Devid Farinelli
  • 7,514
  • 9
  • 42
  • 73
user2495586
  • 253
  • 1
  • 2
  • 6
  • 1
    Does that api accept a jsonp callback parameter? it doesn't appear to currently. If it doesn't, you can't request it using the browser alone. – Kevin B Jun 18 '13 at 03:29
  • this is very comprehensive [3 simple solutions](http://stackoverflow.com/a/15747224/1140227) – George Aug 23 '13 at 09:47
  • Try cross-domain ... check http://www.pureexample.com/jquery/cross-domain-ajax.html – KingRider Apr 15 '16 at 15:16

3 Answers3

2

You can't do this with a browser unless the education.com server is configured to allow CORS (Cross-Origin Resource Sharing) requests (That's the Access-Control-Allow-Origin bit).

Your server could make the request on your behalf, however, and you can then retrieve the data from there.

1

In ZF1 can be done as below:

  public function indexAction() {
    $turnkey = array(
        "uris" => array("176.x.x:3478", "3478"),
        "username" => "my_username",
        "password" => "my_password"
    );

    $content = Zend_Json::encode($turnkey);
    $this->getResponse()
            ->setHeader('Access-Control-Allow-Origin', "*")
            ->setHeader('Content-Type', 'application/json')
            ->setBody($content)
            ->sendResponse();
    exit;
  }
0

Not so much a solution to the problem, but a hack to avoid the problem -- start your browser without web-security:

in MacOS this would work as follows: cd to ../../Applications/Google\ Chrome.app/Contents/MacOS
then start the brwoser with the appropiate flag: Google\ Chrome --disable-web-security'

Note: Never use the browser for normal surfing, this is just to get you going in cases of emergencies! Cheers

Lukas N.P. Egger
  • 923
  • 2
  • 9
  • 12
  • Running `google-chrome --disable-web-security` doesn't solve this problem in Linux. I'd be surprised if this actually works on Mac... – weberc2 Jun 09 '14 at 19:39