簡體   English   中英

如何向每個Angular.js $ http請求添加添加請求參數(例如,啟動xdebug會話)

[英]How to add add request parameter to every Angular.js $http request (to start a xdebug session for example)

我的混合應用程序基於AngularJS並使用php REST api。

我想直接從我的Angular應用程序調試php api,而不是使用REST控制台或Postman。 這將節省大量時間,特別是對於POST和PUT請求。

為此,我需要為每個請求添加一個參數,如下所示:

http://localhost:8000/api/contacts?XDEBUG_SESSION_START=PHPSTORM

我可以配置$ http這樣做嗎?

您可以使用httpInterceptor(官方$ http 文檔包含更多信息)

// register the interceptor as a service
$provide.factory('xdebugInterceptor', function($q) {
  return {
    // optional method
    'request': function(config) {
      // do something on success

      // !!! adjust the config object
      // add request param XDEBUG_SESSION_START=PHPSTORM
      // it will be added to every made request

      config.params = config.params || {};
      config.params.XDEBUG_SESSION_START: "PHPSTORM";

      return config;
    },

    // optional method
   'requestError': function(rejection) {
      // do something on error
      return $q.reject(rejection);
    },


    // optional method
    'response': function(response) {
      // do something on success
      return response;
    },

    // optional method
   'responseError': function(rejection) {
      // do something on error
      return $q.reject(rejection);
    }
  };
});

// make this conditional so you use it only in DEV mode
$httpProvider.interceptors.push('xdebugInterceptor');

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM