angularjs 1.6.4 $ http post授权标题

时间:2017-06-05 14:16:14

标签: angularjs wordpress web-services api

我在postman工具中测试了POST,当添加基本的身份验证标头时,它可以与WordPress API一起使用。当我以角度尝试POST时它不起作用。请帮我解决这个问题。

var app = angular.module("biodata",['base64']);

app.config(function($httpProvider) {        
    $httpProvider.defaults.withCredentials = true;
});

app.controller("biodataController",function($scope,$http,$base64)
{
    $scope.datainsert = function(sendata)
    {
        var login = "pms";
        var password = "N^2&DxDLltBTCdKtUy";

        $scope.sendata = {
            "title":"title3",           
            "acf":{
                "firstname":sendata.firstname,
                "lastname":sendata.lastname,
                "dob":sendata.dob,
                "gender":sendata.gender,
                "qualification":sendata.qualification,
                "experience":sendata.experience,
            }
        }

        var token = $base64.encode(login+':'+password);
        $http.defaults.headers.common['Authorization'] = 'Basic ' + token;
        var test = $http.defaults.headers.common['Authorization'];

        $http({
             method:"POST",
             url:"http://localhost/pms-cms/wp-json/wp/v2/biodata",
             data:$scope.sendata,
             headers:{"Authorization":test}
        }).then(function(data)
        {
            console.log(data);
        });
    }
});

0 个答案:

没有答案