2016-02-25 29 views
1

我正在構建一個首次使用離子的混合應用程序。我必須創建一個將所有控制器連接到外部API的庫。我有認證錯誤。我有以下代碼:離子製作REST API調用

angular.module('starter.services', []) 

.factory('apiConnect',function(){ 
    var sslurl = 'urltoexternal Api'; 
    var apiConnect = function(){ 
     this.vesion = '3.0'; 
     this.prefix = 'oauth_'; 
     this.key = key; 
     this.secret = secret; 
     this.sslurl = '';   
    } 
    apiConnect.prototype.callservice = function(call, $http,$resource) {   
    var endpoint = sslurl + call;   

    $http({ 
     method: 'POST', 
     url: sslurl + call, 
     headers: { 
      'Access-Control-Allow-Origin' : '*', 
      'Access-Control-Allow-Methods' : 'POST, GET, OPTIONS, PUT', 
      'Content-Type': 'application/json', 
      'Accept': 'application/json', 
      'oauth_nonce': '47901059', 
     }, 
     data: { 


     } 
    }).then(function(resp) { 
     console.log('Success', resp); 

     // For JSON responses, resp.data contains the result 
    }, function(err) { 
     console.error('ERR', err); 
     // err.status will contain the status code 
    }) 
} 

我收到以下錯誤

否「訪問控制允許來源」標頭出現在所請求的資源。 我想通過以下

  1. oauth_nonce,oauth_timestamp,oauth_key,oauth_signature在頭中。
  2. 我想調用POST方法,但它總是調用選項
  3. 我想發送和接收json 如何傳遞此值並獲得響應? 這種休息連接的正確方法是什麼?
+1

access-control-allow-origin'頭必須在服務器的響應中,而不是在請求中(請參閱關於CORS的多篇文章,例如http://stackoverflow.com/questions/20035101/no-access-控制允許來源標題目前在請求資源,http://stackoverflow.com/questions/24097484/angular-js-no-access-control-allow-origin-header-is-現在在請求的r,...) – beaver

回答

0

access-control-allow-origin是瀏覽器相關問題,用於離子型REST API消費選項。

您可以輕鬆地使用chrome extension從中解脫。

using CORS extension.

請參閱附卡,使it.Hope它會幫助你享受更多的離子消耗的API。

注:對不起,很晚的答案,因爲我今天得到它

謝謝!