基本身份验证不适用于带有 Ionic/Angular 的 WP API [英] Basic authentication not working for WP API with Ionic/Angular

查看:34
本文介绍了基本身份验证不适用于带有 Ionic/Angular 的 WP API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将这个 Ionic 项目用于我使用 WP API 的地方.现在在我的应用程序中,我希望用户能够提交帖子并将数据保存到 WP DB.我试图通过让我的应用程序自动为每个人验证为 1 个用户来实现这一点.基于以下

I got this Ionic project going where I make use of the WP API. Now in my app I want the user to be able to submit a post and that the data gets saved to the WP DB. I am trying to achieve this by having my app automatically authenticate as 1 user for everyone. Base of the following documentation, I installed the Basic Auth Plugin. I also installed angular-base64. Hooked everything up and run the app but I am still getting a 401 (unauthorised).

我的 HTML 代码的一部分:

Following the WP API V2 documentation on CRUD the api route to use to post anything is the following -> POST /wp-json/wp/v2/posts to create a new Post

<textarea class="customTextarea" name="question" id="" cols="30" rows="30"placeholder="在此处添加问题" ng-model="问题"></textarea><button ng-click="save()">提交</button>

Part of my HTML code:

我的控制器代码的一部分:

Part of my controller code:

.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);
        });


    }

})

请有人帮助我.我忘记了做错了什么.

Please can someone help me out. What have I forgotten of done wrong.

推荐答案

我认为这可能就像您在 Basic 之后缺少一个空格一样简单:

I think it may be as simple as you're missing a space after Basic:

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

这篇关于基本身份验证不适用于带有 Ionic/Angular 的 WP API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆