内容类型image / png ajax

时间:2015-02-04 11:40:18

标签: javascript jquery html ajax

我的服务器请求正在返回image / png内容类型标头。服务器链接如下:http://locahost/PrintView?locate=12,12

我的浏览器显示图片成功。

但我需要通过ajax调用获取图像。但是ajax结果正在编码格式。

enter image description here

所以我不能把它添加到html img或其他元素。

    $.ajax({
        url: "http://localhost:7741/PrintView/Crop",
        type: "GET",
        crossDomain: true,
        data: { "locate":param },            
        success: function (response) {
           $('#crop').html('<img src="' + response + '" />');
           console.log(response)
        },
        error: function (xhr, status) {
            alert("error");
        }
    });

2 个答案:

答案 0 :(得分:4)

您正在尝试从GET请求加载图片。通过在这里涉及Ajax,没有理由使事情复杂化。

$('#crop').empty().append(
    $("<img />").attr(
        "src", 
        "http://localhost:7741/PrintView/Crop?locate=" + encodeURIComponent(param)
    )
);

答案 1 :(得分:0)

如果要使用ajax请求获取该映像,则应首先使用base64对其进行编码,并使用base64字符串响应ajax请求。