如何更改 angularjs $http.jsonp 的标题

2022-01-17 00:00:00 json http-headers angularjs javascript

我阅读了文档.

但我想我一定是误会了.

but I think I must have misunderstood it.

$http.defaults.headers.jsonp = { 'Accept' : 'application/json'};
$http.jsonp(url).success(function(data, status, headers, config) {

我也试过

$httpProvider.defaults.headers.jsonp = { 'Accept' : 'application/json'};
$http.jsonp(url).success(function(data, status, headers, config) {

我想把 Accept 改为 application/json

都不行.

推荐答案

使用 JSONP 时无法控制浏览器发送的标头.JSONP 是一个聪明的技巧(或黑客,取决于你如何看待它......),它包括插入一个指向服务器端点的 <script> 标记.最终,浏览器将决定在通过 <script> 标签请求脚本时发送哪些标头,而您无法影响它.

There is no way to control headers sent by a browser while using JSONP. JSONP is a smart trick (or a hack, depending on how you see it...) that consist of inserting a <script> tag pointing to a server endpoint. Ultimately it is a browser who will decide which headers to sent while requesting scripts via <script> tag and you can't influence it.

在此处了解更多信息:修改 JSONP 请求的 HTTP 标头

相关文章