2015-11-04 53 views
0

我使用json api auth從我的移動應用程序登錄到wordpress,它的工作原理,但是當我想從應用程序創建新的帖子,我做了發佈請求它說我錯誤,我沒有許可;它看到我沒有登錄的用戶,爲什麼?Json Api Auth發送請求到wordpress後端

這是我的代碼:

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


    var cookie=AuthCookieStorage.get(); 
    console.log("cookie" + " " + cookie); 

    var data={"title":"Hello World!","content_raw":"Content","excerpt_raw":"Excerpt"}; 

    $scope.register = function() { 

     $http({ 
      method: 'POST', 
      url: 'http://url.com/provawp/wp-json/wp/v2/posts', 
      data: data, 
      headers: {'Content-Type': 'application/json', 
         'Authorization' : cookie 
      } 
     }) 
     .success(function(data, status, headers, config) { 
      alert("Città aggiunta correttamente!"); 
     }) 
     .error(function(data, status, headers, config) { 
     }). 
     catch(function (error) { 
     alert("Errore!"); 
     console.log("error : " + JSON.stringify(error)); 

     }); 
}  }); 

回答

1

我認爲,當通過Authorization頭,你需要前綴令牌Bearer

$http({ 
    method: 'POST', 
    url: 'http://url.com/provawp/wp-json/wp/v2/posts', 
    data: data, 
    headers: { 
     'Content-Type': 'application/json', 
     'Authorization' : 'Bearer ' + cookie //added 'Bearer ' 
    } 
})