在向Ajax调用Restful Web服务时获取错误请求错误

时间:2014-05-13 14:00:25

标签: java ajax json rest jaxb

我正在尝试使用以下java脚本代码将json对象转换为xml文档:

function createXML(oObjTree) {
function loadObjTree(oParentEl, oParentObj) {
    var vValue,
    oChild;
    if (oParentObj instanceof String || oParentObj instanceof Number || oParentObj instanceof Boolean) {
        oParentEl.appendChild(oNewDoc.createTextNode(oParentObj.toString()));
    } else if (oParentObj.constructor === Date) {
        oParentEl.appendChild(oNewDoc.createTextNode(oParentObj.toGMTString()));
    }
    for (var sName in oParentObj) {
        if (isFinite(sName)) {
            continue;
        }
        vValue = oParentObj[sName];
        if (sName === "keyValue") {
            if (vValue !== null && vValue !== true) {
                oParentEl.appendChild(oNewDoc.createTextNode(vValue.constructor === Date ? vValue.toGMTString() : String(vValue)));
            }
        } else if (sName === "keyAttributes") {
            for (var sAttrib in vValue) {
                oParentEl.setAttribute(sAttrib, vValue[sAttrib]);
            }
        } else if (sName.charAt(0) === "@") {
            oParentEl.setAttribute(sName.slice(1), vValue);
        } else if (vValue.constructor === Array) {
            for (var nItem = 0; nItem < vValue.length; nItem++) {
                oChild = oNewDoc.createElement(sName);
                loadObjTree(oChild, vValue[nItem]);
                oParentEl.appendChild(oChild);
            }
        } else {
            oChild = oNewDoc.createElement(sName);
            if (vValue instanceof Object) {
                loadObjTree(oChild, vValue);
            } else if (vValue !== null && vValue !== true) {
                oChild.appendChild(oNewDoc.createTextNode(vValue.toString()));
            }
            oParentEl.appendChild(oChild);
        }
    }
}
const oNewDoc = document.implementation.createDocument("", "", null);
loadObjTree(oNewDoc, oObjTree);
return oNewDoc;

}; 现在,我想使用java脚本中的Ajax调用将转换后的xml文档发送到一个安静的Web服务 我的ajax电话如下:

     var lookupReq = (window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP"));
    var xmlDoc= createXML(lookupJson);
//make the ajax call to hit the web service   
lookupReq.send(xmlDoc); 

我的网络服务如下:

@Path("/ImageServlet")

@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, MediaType.TEXT_XML})

public class ImageViewerServlet {



    @POST
    @Path("/lookup")
    @Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, MediaType.TEXT_XML})
    public Response getObjectId(GAIssuesLookupArg gaArg)
    {
        System.out.println("gaArgs : "+gaArg);
        return null;
    }

} 

在Web服务端,我使用JAXB进行编组和解组。 但是,当我尝试使用上面提到的ajax调用来访问Web服务时,我收到错误请求错误。任何人都可以告诉我我在做错误并更正我的代码吗? 如果我的问题不明确,请告诉我。提前谢谢。

0 个答案:

没有答案
相关问题