Ajax 使用AngularJS或jQuery获取内容类型
我想用AngularJS做一个指令,它显示媒体(图像或视频) 为了实现这一目标,我必须确定媒体的类型,为了做到这一点,我想对url进行AJAX请求并获取内容类型标题,但我在文档中没有找到类似的内容Ajax 使用AngularJS或jQuery获取内容类型,ajax,jquery,angularjs,content-type,Ajax,Jquery,Angularjs,Content Type,我想用AngularJS做一个指令,它显示媒体(图像或视频) 为了实现这一目标,我必须确定媒体的类型,为了做到这一点,我想对url进行AJAX请求并获取内容类型标题,但我在文档中没有找到类似的内容 有什么提示吗?您可以通过以下方式获得所有标题: 可以使用jQuery执行此操作: $.ajax({ url:'/someUrl', type: 'GET', success:function(res, status, xhr){ var contentType
有什么提示吗?您可以通过以下方式获得所有标题:
可以使用jQuery执行此操作:
$.ajax({
url:'/someUrl',
type: 'GET',
success:function(res, status, xhr){
var contentType = xhr.getResponseHeader('Content-Type');
}
});
经过两个多小时的搜索,这是我为angularJS 1.x找到的最佳答案:
$http({
method: method,
url: url,
data: data,
header: {'Content-Type': 'application/x-www-form-urlencoded'},
timeout: 30000
})
.then(
//success function
function(response){
console.log(response.headers('content-type'));
},
//error function
function(response){
}
);
主要部分是response.headers('content-type')
注1:此解决方案适用于角度1.x
注2:此方法使用的是然后(成功函数,错误函数),这是比旧方法(成功和错误分开)更好、更可靠的
$http({
method: method,
url: url,
data: data,
header: {'Content-Type': 'application/x-www-form-urlencoded'},
timeout: 30000
})
.then(
//success function
function(response){
console.log(response.headers('content-type'));
},
//error function
function(response){
}
);