即使我返回200,我的Ajax POST也会出现错误

时间:2013-04-19 19:59:27

标签: ajax jersey

我正在向Jersey资源发布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

        }
    });

1 个答案:

答案 0 :(得分:1)

设置dataType: "json",表示您的回复是json,这不会导致错误,因此请将其删除。
您的图像数据也必须进行base64编码才能生成数据uri。