8

I am trying to query wolfram to do some math for my site and then display the result. I am having trouble with CORS. My Code:

var xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function() { 
  if (xmlHttp.readyState == 4 && xmlHttp.status == 200)
     callback(xmlHttp.responseText);
}
xmlHttp.open("GET", "http://api.wolframalpha.com/v2/query?input="+theUrl+"&appid=", true); // true for asynchronous 
xmlHttp.send(null);

My Error:

"Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://api.wolframalpha.com/v2/query?input=sqrt(100)&appid=. (Reason: CORS header 'Access-Control-Allow-Origin' missing)."

I understand that on a dynamic site I could just add

Header set Access-Control-Allow-Origin "*"

to .htaccess

but I'm not sure how to do it on a static site. I have read that Allow-Access_origin should already be present in github pages.

2nd answer here: Cross-Origin Resource Sharing on GitHub Pages

2nd answer here: Is there a way to enable CORS on Github pages?

Community
  • 1
  • 1
Seth Kitchen
  • 1,526
  • 19
  • 53
  • 1
    The Access-Control-Allow-Origin Header must be set on the server side, i.e. wolframalpha in this case. No way for you to do it probably unless you administer wolframalpha. – Stefan Hegny Sep 05 '16 at 07:46
  • This is a duplicate of http://stackoverflow.com/questions/16268930/wolfram-api-javascript-cross-origin-sharing-issue . Please remove the bounty and close the question. – Andy Ray Sep 08 '16 at 19:02
  • @AndyRay that question has no answer. Mine is specifically different because it asks for alternatives – Seth Kitchen Sep 08 '16 at 19:08

1 Answers1

2

If it's a small project you could route your get requests through crossorigin.me. Otherwise you'll have to run a server yourself that proxies requests to wolfram alpha and sets the Access-Control-Allow Origin header properly. You could even deploy one of these proxy servers on now.sh or heroku for free or cheap. I have a similar simple application github-issue-filer that sets the header properly and reroutes POSTs to github's API.

Drew Schuster
  • 2,521
  • 12
  • 15
  • I think I will use the crossorigin.me if no better solution is given. Why must that be a "small project"? Thanks! – Seth Kitchen Sep 09 '16 at 17:18
  • 2
    The owner of crossorigin.me is running the site for free as a courtesy, so if you're driving tons of traffic it'd be kind to not abuse their generosity :-) – Drew Schuster Sep 10 '16 at 00:27