2016-05-25 28 views
1

我得到了這個Ionic項目去了我使用WP API的地方。現在在我的應用程序中,我希望用戶能夠提交帖子並將數據保存到WP數據庫。我試圖通過讓我的應用自動以每個人1個用戶的身份進行身份驗證來實現這一目標。以下documentation的基地,我安裝了Basic Auth Plugin。我也安裝了angular-base64。把所有東西都掛起來,運行應用程序,但我仍然得到401(未經授權)。基本身份驗證不適用於帶有Ionic/Angular的WP API

WP API V2 documentation on CRUD的API路線用來張貼任何在以下 - > POST/WP-JSON/WP/V2 /職位,以創建一個新的崗位我的HTML代碼

enter image description here

部分:我的控制器代碼

<div class="padding"> 
     <textarea class="customTextarea" name="question" id="" cols="30" rows="30" 
     placeholder="Add Question Here" ng-model="question"></textarea> 

     <button ng-click="save()">Submit</button> 
    </div> 

部分:

.controller('AddQuestionCtrl', function($scope, $http, $base64){ 
    $scope.save = function(){ 
     console.log('SAVE'); 
     var url = 'http://XXXXX/wp-json/wp/v2/posts'; 
     var username = 'XXXX'; 
     var psw2 = 'XXXXX'; 

     $http.post(url, {question: $scope.question},{ 
      headers:{ 
       'Content-Type' : 'application/x-www-form-urlencoded; charset=UTF-8', 
       'Authorization' : 'Basic' + $base64.encode(username + ':' + psw2), 
       'Access-Control-Allow-Origin': '*' 
      } 
     }).then(function(response){ 
      $scope.result = response; 
      console.log('The data has been saved to DB', $scope.result); 
     }); 


    } 

}) 

請有人幫助我。我忘了做錯了什麼。

回答

1

我想,你Basic後失蹤的空間也可能是簡單的:

'Authorization' : 'Basic ' + $base64.encode(username + ':' + psw2), 
+0

不錯趕上!我在想也許是CSRF的一些事情。 – robjam