ajax接收image_jpeg,javascript - AJAX request png/jpeg image from (NodeJS + Express) server and show it...

basically I need to receive png or jpeg image from my server, and show it inside img tag on my website.

My architecture looks like this:

Client - Server1 (my server) - Server2 (some public server)

Client sends Ajax request to Server1.

Server1 sends request to Server2.

Server2 sends image back to Server1.

Server1 sends image back to Client.

Client code:

$("#testButton").click(function() {

$.ajax({

method: "get",

async: false,

url: "/test"

}).done(function (response){

alert(response.imageData);

$("#resultImage").attr("src", "data:image/png;base64," + response.imageData);

});

});

Server1 code:

router.get('/test', function(req, res, next){

var url = 'https://ion.gemma.feri.um.si/ion/services/geoserver/demo1/wms?service=WMS&version=1.1.0&request=GetMap&layers=demo1%3Amaribor-dof25&bbox=546631.6237364038%2C156484.86830455417%2C550631.7865026393%2C160485.0310707898&width=767&height=768&srs=EPSG%3A3794&format=image%2Fjpeg';

request(url, function (error, response, body) {

console.log('error:', error);

console.log('statusCode:', response && response.statusCode);

console.log(response.headers['content-type']);

console.log(body);

return res.json({ imageData: body});

});

});

If I enter the url above directly to img src, it is shown correctly.

Image is also shown correctly when I input url directly into browser.

When I receive image data back to my client from server1, data looks like this:

tp4MS.png

Any ideas how to fix this ?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值