1

我正在向 Jersey Resource 发布 AJAX 帖子。我建立一个图像并返回它。问题是,无论我返回什么状态,Ajax 都认为这是一个错误。我实际上可以使用错误消息(通过error.responseTest),它工作正常。但它很丑。我要返回什么才能使它成功?我已返回 OK(200)、Accepted(202) 和 Created(201)。他们都给我一条错误消息,但在我的控制台网络选项卡中,我得到了成功(绿色,状态正确)。

我像这样返回它:

    return Response.status(Response.Status.ACCEPTED).entity(image).header('Content-Type',"image/png").build();

我的 JS 代码:

$.ajax( Monocle.config.Sightline.jobUrl + "/sightline", {
        type: "POST",
        processData: false,
        data: JSON.stringify({
            Lat1: Monocle.Sightline.BBOX(feature,2),
            Long1: Monocle.Sightline.BBOX(feature,1),
            Lat2: Monocle.Sightline.BBOX(feature,4),
            Long2: Monocle.Sightline.BBOX(feature,3),
            OrgLat:observerCoords[0].lat,
            OrgLong:observerCoords[0].lon,
            ObHeight: feature.attributes.observerHeight,
            TargHeight: feature.attributes.targetHeight,
            OuterRadius: feature.attributes.outerRadius,
            LVA: feature.attributes.lowerVertAngle,
            UVA: feature.attributes.upperVertAngle,
            sAzimuth: feature.attributes.startAzimuth,
            eAzimuth: feature.attributes.endAzimuth,
            outputType: "MAX"
        }),
        contentType: "application/json",
        dataType: "json",
        success: function( results ){
            var graphic = new OpenLayers.Layer.Image(
                Monocle.currentWidget.name + " Destination " + featurenum,
                "data:image/png;base64," + results,
                new OpenLayers.Bounds(Monocle.Sightline.BBOX(feature,1), Monocle.Sightline.BBOX(feature,2), Monocle.Sightline.BBOX(feature,3),Monocle.Sightline.BBOX(feature,4)),
                new OpenLayers.Size(580, 288),
                { isBaseLayer: false,
                    opacity: 0.3,
                    displayOutsideMaxExtent: true
                });
            feature.legionObject = graphic;
            graphic.relatedlayer = Monocle.currentWidget.name + " Destination " + featurenum;

            Monocle.Map.map.addLayer(graphic);
        },
        error: function(errMsg) {
            // TODO: really handle any errors

        }
    });
4

1 回答 1

1

设置dataType: "json",意味着您的响应是json不会导致错误的,因此只需将其删除。
此外,您的图像数据必须经过 base64 编码才能生成数据 uri。

于 2013-04-19T20:22:04.457 回答