【问题标题】:Angular $response GET request removes content typeAngular $response GET 请求删除内容类型
【发布时间】:2016-09-04 21:58:06
【问题描述】:

这是我的资源

this.users = $resource(AppConstants.baseUrl);

这是我的拦截器

return {
      request : function(config){

        if(blackList.indexOf(config.url.index) != -1){ //Skip API which does not demand JSON type
          config.headers["Content-Type"] = "application/json";  
        }

        //Delete content type for GET request
        if (!config.data) {
          console.log('Setting content type')
          delete config.headers['Content-Type'];
          config.headers["Content-Type"] = "application/json;charset=utf-8";  
        }
        console.log(config)

        return config;
      },

      response : function(response){
        return response;
      }
    };

现在,每当我发出 GET 请求时,Content-Type 都会从 Request 中适当地删除。我什至通过了虚拟数据,但仍然没有运气。不幸的是,我的 REST API 要求明确提及 Content-Type

我错过了什么!

【问题讨论】:

标签: javascript angularjs get http-headers content-type


【解决方案1】:

想通了。它是臭名昭著的角度格式,如果数据为空,请删除 Content-Type

解决方法是,更改 HTTP 拦截器。在我的问题中提到拦截器,我添加了一行

 if (!config.data) {
          console.log('Setting content type')
          delete config.headers['Content-Type'];
          config.headers["Content-Type"] = "application/json;charset=utf-8";  
          config.data = { dummy : "dummy" }; //Send a dummy data so that your content-type is retained
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-31
    • 2012-04-23
    • 1970-01-01
    • 2016-01-01
    • 2016-04-08
    • 2011-08-05
    相关资源
    最近更新 更多