0

我正在尝试通过 Javascript 使用 WolframAlpha 的 API。我不明白为什么下面的代码会给我一个空警报。我是 jQuery 的新手,但有人告诉我这是在这里使用的最佳方法。

<html>
  <head>
    <script src="jquery-2.1.1.min.js"></script>
  </head>
    <body>
        <script>
        $.get('http://api.wolframalpha.com/v2/query?appid=77P9PL-2QAWEY86WV&input=2x&format=image,plaintext', function(responseText) {
        alert(responseText);
        });
        </script>
    </body>
</html>

非常感谢您的帮助

4

1 回答 1

0

所以这受到称为跨源脚本的安全机制的影响。这里的问题是您调用的服务没有允许您在 ajax 调用中调用该资源。

要阅读更多内容,请查看http://en.wikipedia.org/wiki/Cross-origin_resource_sharing

为了证明这种情况正在发生,请查看 JS Fiddle:http: //jsfiddle.net/dgohczvf/1/

您可以在控制台中看到:

(index):1 XMLHttpRequest cannot load http://api.wolframalpha.com/v2/query?appid=77P9PL-2QAWEY86WV&input=2x&format=image,plaintext. 
No 'Access-Control-Allow-Origin' header is present on the requested resource. 
Origin 'http://fiddle.jshell.net' is therefore not allowed access.

这里的另一个答案建议您需要将此服务器调用到服务器: Wolfram API javascript cross originsharing issue

于 2014-11-28T00:34:25.317 回答