html的头部添加图片,在JavaScript中添加HTTP头到图像请求

在角1.2.X

有很多方法可以做到这一点。在角度1.2中,我建议使用http interceptor“擦除”传出请求并添加标题。

// An interceptor is just a service.

app.factory('myInterceptor', function($q) {

return {

// intercept the requests on the way out.

request: function(config) {

var myDomain = "http://whatever.com";

// (optional) if the request is heading to your target domain,

// THEN add your header, otherwise leave it alone.

if(config.url.indexOf(myDomain) !== -1) {

// add the Authorization header (or custom header) here

config.headers.Authorization = "Token 12309123019238";

}

return config;

}

}

});

app.config(function($httpProvider) {

// wire up the interceptor by name in configuration

$httpProvider.interceptors.push('myInterceptor');

});

在角1.0.X

如果您使用Angular 1.0.X,则需要在标头中更全面地设置标题… $ http.defaults.headers.common.Authentication

编辑:来自的东西

为此,您需要创建一个指令,这可能会变得奇怪。

您需要:

>创建一个指令,该指令位于您的< img />标记,或创建它。

>该指令使用$ http服务请求映像(从而利用上述http拦截器)。为此,您将需要检查扩展名并设置适当的内容类型标题,如:$ http({url:’images / foo.jpg’,headers:{‘content-type’:’image / jpeg’})然后(…)

>当您得到响应时,您必须使用raw base64数据,并将image元素的src属性设置为数据src,如下所示:< img src =“data:image / jpeg; base64,9hsjadf9ha9s8dfh .. .asdfasfd“/取代。

所以会变得疯狂。

如果可以使您的服务器不会保护您更好的图像。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值