简体   繁体   English

使用Ajax调用来调用Alchemy API

[英]Calling Alchemy API using ajax call

I am trying to use ajax to call the Alchemy REST API, but I am getting an injector error which I have mentioned below. 我正在尝试使用Ajax调用Alchemy REST API,但是我遇到了下面提到的注射器错误。 The following is the code in my HTML file. 以下是我的HTML文件中的代码。 Looking for suggestions to solve the error. 寻找解决错误的建议。

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" ></meta>
<script language="JavaScript" type="text/javascript" src="http://code.jquery.com/jquery-2.1.0.min.js"></script>
</head>
<body>

Alchemy trial
<span id="yammer-login"></span>
<script>
    $.ajax({
        url: 'http://access.alchemyapi.com/calls/url/URLGetRankedKeywords',
        type: 'GET',
        data: 'url=http://yurishelke.weebly.com&apikey=apikey&maxRetrieve=1', // or $('#myform').serializeArray()
        success: function(keyword) { 
            //document.getElementById('yammer-login').innerHTML = keyword;
            console.dir(keyword);
            alert('PUT completed');  
        }

    });
</script>
</body>
</html>

I am getting the following error in the console :- 我在控制台中收到以下错误:-

[21:39:07.317] Error: [$injector:unpr] http://errors.angularjs.org/undefined/$injector/unpr?p0=AngularIssuesProvider%20%3C-%20AngularIssues
L/<@https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0/angular.min.js:6
Xb/l.$injector<@https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0/angular.min.js:32
c@https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0/angular.min.js:29
Xb/r.$injector<@https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0/angular.min.js:32
c@https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0/angular.min.js:29
d@https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0/angular.min.js:30
Xb/f/<.instantiate@https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0

It might be that AlchemyAPI returns multiple CORS headers like this: AlchemyAPI可能会返回多个CORS标头,如下所示:

Access-Control-Allow-Origin: *
Access-Control-Allow-Origin: *

Which is actually interpreted in the browser like this: 在浏览器中实际上是这样解释的:

Access-Control-Allow-Origin: *, *

Which is invalid per the standard, and improperly interpreted by the browser. 根据标准,这是无效的,并且浏览器无法正确解释。

That aside, you probably shouldn't be hitting AlchemyAPI directly. 除此之外,您可能不应该直接使用AlchemyAPI。 Instead, you should be using a web server to proxy the API calls, and hitting that server with AJAX. 相反,您应该使用Web服务器代理API调用,并使用AJAX来访问该服务器。 By putting the AJAX call to the API client-side, you're giving out your key to any person or device that uses it, and now, you've given it to anyone that views this question on Stack Exchange. 通过将AJAX调用传递给API客户端,您会将密钥分发给使用它的任何人或设备,现在,您已将其提供给在Stack Exchange上查看此问题的任何人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM