Angular $ response GET请求删除内容类型

时间:2016-05-10 05:15:31

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

这是我的资源

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

我错过了什么!

1 个答案:

答案 0 :(得分:0)

想出来。它是臭名昭着的角度格式,如果数据为空,则删除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
        }