gpt4 book ai didi

javascript - 如何在静态 Github 页面上发出 WolframAlpha 请求?

转载 作者:数据小太阳 更新时间:2023-10-29 05:32:25 24 4
gpt4 key购买 nike

我正在尝试查询 wolfram 为我的网站做一些数学运算,然后显示结果。我在使用 CORS 时遇到了问题。我的代码:

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

我的错误:

“跨源请求被阻止:同源策略不允许读取位于 http://api.wolframalpha.com/v2/query?input=sqrt(100)&appid= 的远程资源。(原因:缺少 CORS header ‘Access-Control-Allow-Origin’)。”

我知道在动态网站上我可以添加

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

到.htaccess

但我不确定如何在静态站点上执行此操作。我读过 Allow-Access_origin 应该已经存在于 github 页面中。

这里的第二个答案:Cross-Origin Resource Sharing on GitHub Pages

这里的第二个答案:Is there a way to enable CORS on Github pages?

最佳答案

如果这是一个小项目,您可以通过 crossorigin.me 路由您的获取请求.否则,您必须自己运行一个服务器来代理对 wolfram alpha 的请求并正确设置 Access-Control-Allow Origin header 。您甚至可以免费或廉价地在 now.sh 或 heroku 上部署这些代理服务器之一。我有一个类似的简单应用程序 github-issue-filer正确设置 header 并将 POST 重新路由到 github 的 API。

关于javascript - 如何在静态 Github 页面上发出 WolframAlpha 请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39320637/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com