gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 06:02:55 24 4
gpt4 key购买 nike

这是我的资源

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 请求时,内容类型都会从请求中适当删除。我什至传递了虚拟数据,但仍然没有运气。不幸的是,我的 REST API 要求明确提及 Content-Type

我错过了什么!

最佳答案

想通了。它是臭名昭著的 Angular 格式,如果数据为空,则删除 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
}

关于javascript - Angular $response GET 请求删除内容类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37129780/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com