简体   繁体   English

$ http请求中的角度js默认预定义数据

[英]angular js default predefined data in $http request

I need a way to add a default data attribute to a http request 我需要一种将默认数据属性添加到http请求的方法
I want something like this 我想要这样的东西

var app = angular.module('dumbhttp', []);

app.service('dumbhttp', function ($http, $location) {
    this.post = function(url, data){
        data.prePopData = 'sameInfoForEveryRequest';
        return $http.post(url, data);
    };
});

this works but it doesn't seem like the right way to do things. 这行得通,但似乎做事方式不正确。
I have to use the service dumbhttp insted of the angulars $http 我必须使用角度为$ http的dumbhttp服务

If this worked it would be great! 如果成功了,那就太好了!

var app = angular.module('$http', []);

app.service('$http', function ($http, $location) {
    this.post = function(url, data){
        data.prePopData = 'sameInfoForEveryRequest';
        return $http.post(url, data);
    };
});

but alas it doesn't :-( 但可惜不是:-(

maybe something like this could be the right way to do this 也许像这样的事情可能是正确的方法

$http.setDefaluts.requestData.mykey = 'mydefaultvalue'

Im not using this in a RESTful way just heads up, out team uses a different architecture 我没有以RESTful的方式使用它,只是团队使用了不同的架构

Thanks in advance! 提前致谢!
-James -詹姆士

You need to use an interceptor: 您需要使用拦截器:

$httpProvider.interceptors.push(function($q, dependency1, dependency2) {
    return {
     'request': function(config) {
         config.data.prePopData = 'sameInfoForEveryRequest';
         return config;
      }
    };
  });

You want to take a look at $resource service 您想看看$ resource服务

https://docs.angularjs.org/api/ngResource/service/ $resource https://docs.angularjs.org/api/ngResource/service/ $ resource

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM