401未经授权的回复 [英] 401 Unauthorized Response On Post

查看:137
本文介绍了401未经授权的回复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用角度模板来制作node.js网站。
但是我无法在数据库(Mongo)上创建数据。
这里是代码。



节点路由。

  var Property = mongoose.model('Property'); 
var jwt = require('express-jwt');
var auth = jwt({
secret:'SECRET',
userProperty:'payload'
});
var Schema = mongoose.Schema;
var ObjectId = Schema.ObjectId; //还尝试过Schema.Types.ObjectId,mongoose.ObjectId

//属性路由管理

router.post('/ property',auth,function(req, res,next){
var property = new Property(req.body);
console.log(checkpoint api);
property.save(function(err,property){
if(err){
return next(err);
}
return res.json(property);
})
});

AngularJS HTML

 code>< form class =form-horizo​​ntal> 

< label class =col-md-3 control-labelfor =title>成员名称:< / label>
< div class =col-md-5>
< input id =notific8_texttype =textclass =form-controlvalue =placeholder =ng-model =property.user>
< / div>
< button ng-click =updateProperty()>提交< / button>
< / form>

AngularJS控制器。



pre> angular.module('MainApp')。controller('EditPropertyController',[
'$ scope',
's_property',
's_auth',
'$ state',
'$ location',
函数($ scope,s_property,s_auth,$ state,$ location){
$ scope。 updateProperty = function()
{
var data = {
user:$ scope.property.user,
}
s_property.create(data);
};
}]);

和AngularJS服务。

  angular.module('MetronicApp')
.factory('s_property',[
'$ http',
'$ window',
' s_auth',
函数($ http,$ window,s_auth){
var service = {
all_properties:[],
current_property:{}
};

service.create = function(property){
console.log(checkpoint service);
return $ http.post('/ api / v1 / property' );
};
};
]);

当我输入输入标签中的数据并点击提交按钮时,chrome控制台显示如下



  POST http:// localhost:3000 / property 401(未经授权)

有什么错误?
干杯。



PS



这是



factory('s_auth',''$ http','$ window',''''''''''''函数($ http,$ window){var auth = {}; auth.saveToken = function(token){$ window.localStorage ['current-user-token'] = token;}; auth.getToken = function() return $ window.localStorage ['current-user-token'];};}



service

解决方案

看起来你没有将JWT令牌发送到你的后端
要发送请尝试在您的发帖请求中添加标头{授权:{TOKEN}}


I've been making node.js website using angular template. But I can't create data on DB(Mongo). Here are code.

node routing.

var Property = mongoose.model('Property');
var jwt = require('express-jwt');
var auth = jwt({
  secret: 'SECRET',
  userProperty: 'payload'
});
var Schema = mongoose.Schema;
var ObjectId = Schema.ObjectId; //Have also tried Schema.Types.ObjectId, mongoose.ObjectId

// Property Routing Management

router.post('/property', auth, function(req, res, next) {
  var property = new Property(req.body);
  console.log("checkpoint api");
  property.save(function(err, property) {
    if (err) {
      return next(err);
    }
    return res.json(property);
  })
});

AngularJS HTML

<form class="form-horizontal">

  <label class="col-md-3 control-label" for="title">Member Name:</label>
  <div class="col-md-5">
    <input id="notific8_text" type="text" class="form-control" value="" placeholder="" ng-model="property.user">
  </div>
  <button ng-click="updateProperty()"> Submit </button>
</form>

AngularJS Controller.

angular.module('MainApp').controller('EditPropertyController', [
        '$scope',         
        's_property', 
        's_auth', 
        '$state', 
        '$location',
        function($scope, s_property, s_auth, $state, $location) {
    $scope.updateProperty = function()
    {
        var data = {
            user: $scope.property.user,            
        }
        s_property.create(data);
    };
}]);

and AngularJS service.

angular.module('MetronicApp')
  .factory('s_property', [
    '$http',
    '$window',
    's_auth',
    function($http, $window, s_auth) {
      var service = {
        all_properties: [],
        current_property: {}
      };

      service.create = function(property) {
        console.log("checkpoint service");
        return $http.post('/api/v1/property', property);
      };
    };
  ]);

When I input data in the input tag and click the submit button, chrome console shows like this

POST http://localhost:3000/property 401 (Unauthorized)

What is the mistake? Cheers.

PS

this is

angular.module('MainApp')
.factory('s_auth', ['$http', '$window', function($http, $window){
	var auth = {};

	auth.saveToken = function (token) {
		$window.localStorage['current-user-token'] = token;
	};

	auth.getToken = function() {
		return $window.localStorage['current-user-token'];
	};
 }

service

解决方案

Looks like you are not sending the JWT token to your backend. TO be sending it try adding headers { authorization: "{TOKEN}"} in your post request

这篇关于401未经授权的回复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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