我使用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) );
});
} });
答案 0 :(得分: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 '
}
})