2016-11-25 270 views
0

我使用Yandex-transleyt,並根據請求收到答覆「API密鑰無效」。它只是收到鑰匙。關於查詢格式的議會,這個主題沒有幫助(Yandex API Translator error - 401 API key is invalid)。 請求格式作爲指導:https://tech.yandex.com/translate/doc/dg/reference/translate-docpage/API密鑰無效Yandex Translate

我的要求:

var url = "https://translate.yandex.net/api/v1.5/tr.json/translate?"; 
var keyAPI = "trnsl.1.1.20161125T152027Z.65e73e18a60f3051.7764a7a11754995544ad557501df9e14abbfb6e0"; 

var params = "key=" + keyAPI + "&text=" + textApi + "&lang=" + langApi; 
var vm = this; 

$http({ 
    url: url, 
    method: 'POST', 
    params: params 
}); 

也許錯了請求參數?

完整的代碼here或在這裏:

\t var app = angular.module('jsbin', []); 
 

 
app.controller('DemoCtrl', function($scope, $http) { 
 

 
\t $scope.SendData = function() { 
 
\t \t // тут данные 
 
\t \t var textApi = 'Hello'; 
 
\t \t var langApi = 'en-ru'; 
 
\t \t var url = "https://translate.yandex.net/api/v1.5/tr.json/translate?"; 
 
\t \t var keyAPI = "trnsl.1.1.20161125T152027Z.65e73e18a60f3051.7764a7a11754995544ad557501df9e14abbfb6e0"; 
 

 
\t \t var params = "key=" + keyAPI + "&text=" + textApi + "&lang=" + langApi; 
 
\t \t var vm = this; 
 

 
\t \t $http({ 
 
\t \t \t \t url: url, 
 
\t \t \t \t method: 'POST', 
 
\t \t \t \t params: params 
 
\t \t \t }) 
 
\t \t \t .success(function(data, headers, status, config) { 
 
\t \t \t \t $scope.PostDataResponse = data; 
 
\t \t \t \t vm.data = data; 
 
\t \t \t \t console.log(data); 
 
\t \t \t }) 
 
\t \t \t .error(function(data, headers, status, config) { 
 
\t \t \t \t $scope.ResponseDetails = "Data: " + data + 
 
\t \t \t \t \t "<hr />status: " + status + 
 
\t \t \t \t \t "<hr />config: " + config; 
 
\t \t \t \t console.log("error1"); 
 
\t \t \t \t console.log(data); 
 
\t \t \t \t console.log(status); 
 
\t \t \t \t console.log(config); 
 
\t \t \t }); 
 
\t }; 
 
});
<!DOCTYPE html> 
 
<html> 
 
<head> 
 
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script> 
 
    <meta charset="utf-8"> 
 
    <title>Angular JS</title> 
 
    </head> 
 
<body ng-app="jsbin"> 
 
    <div ng-controller="DemoCtrl as vm"> 
 
    
 
      <button ng-click="SendData()">Send</button> 
 
    <br> 
 
     Data: {{PostDataResponse}}<br> 
 
    {{vm.data}} 
 
    {{vm.PostDataResponse}} 
 
    Data: {{scope.PostDataResponse}} 
 
    {{vm.data}} 
 
    </div> 
 
    </body> 
 
    </html>

回答

0

現在它的工作!謝謝大家。

var app = angular.module('jsbin', []); 
 

 
app.controller('DemoCtrl',function($scope, $http){ 
 
    
 
    $scope.SendData = function() { 
 
      // тут данные 
 
     var textApi='Hello'; 
 
     var langApi='en-ru'; 
 
     var url = "https://translate.yandex.net/api/v1.5/tr.json/translate?"; 
 
    var keyAPI ="trnsl.1.1.20161125T152027Z.65e73e18a60f3051.7764a7a11754995544ad557501df9e14abbfb6e0"; 
 
    
 
     params ='key='+keyAPI+'&text='+textApi+'&lang='+langApi; 
 
    var vm = this; 
 
     $scope.method = 'POST'; 
 
    $scope.url = 'https://translate.yandex.net/api/v1.5/tr.json/translate?'; 
 
     
 
    $http({ 
 
    url: url, 
 
    method:"POST", 
 
    params:{key:keyAPI,text:textApi,lang:langApi}, 
 
}) 
 
.success(function(data,headers,status,config){ 
 
    $scope.PostDataResponse = data; 
 
    vm.data = data; 
 
    console.log(data); 
 
}) 
 
.error(function(data,headers,status,config){ 
 
$scope.ResponseDetails = "Data: " + data + 
 
       "<hr />status: " + status + 
 
        "<hr />config: " + config; 
 
     console.log("error1"); 
 
     console.log(data); 
 
     console.log(status); 
 
     console.log(config) 
 
    } 
 
) 
 
} 
 
} 
 
);
<!DOCTYPE html> 
 
<html> 
 
<head> 
 
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script> 
 
    <meta charset="utf-8"> 
 
    <title>Angular JS</title> 
 
    </head> 
 
<body ng-app="jsbin"> 
 
    <div ng-controller="DemoCtrl as vm"> 
 
    
 
      <button ng-click="SendData()">Send</button> 
 
    <br> 
 
     Data: {{PostDataResponse}}<br> 
 
    {{vm.data}} 
 
    {{vm.PostDataResponse}} 
 
    Data: {{scope.PostDataResponse}} 
 
    {{vm.data}} 
 
    </div> 
 
    </body> 
 
    </html>