Ajax Post Header

2019-06-04 18:57发布

问题:

I have the Ajax Call for the Post method and it consumes XML and Produces PDF. How do i set my header in AJAX? Any inputs on the exception would be a great help.I did browse, but did not found much on this.

Ext.Ajax.request({
        url : "/rest/sample/" + sampleId + "/sam",
        params : params,
        form : downloadForm,
        isUpload : true,
        method : 'POST',
        headers: {'Content-Type': 'text/xml'},
        scope : this,
        xmlData: XMLSampleData
    }); 

XMLSampleData has the Xml data.

Rest :

  @Path("/sam")
    @Produces("application/pdf")
    @Consumes({"application/xml", "text/xml"})

I keep getting this exception:

Uncaught REST Server Error: null
javax.ws.rs.WebApplicationException
    at com.sun.jersey.server.impl.uri.rules.TerminatingRule.accept(TerminatingRule.java:66)
    at com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:86)
    at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:136)
    at com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:74)
    at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1357)
    at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1289)
    at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1239)
    at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1229)
    at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:420)
    at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:497)
    at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:684)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
    at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:487)
    at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:362)
    at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
    at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:181)
    at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:729)
    at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:405)
    at org.mortbay.jetty.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:206)
    at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
    at org.mortbay.jetty.Server.handle(Server.java:324)
    at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:505)
    at org.mortbay.jetty.HttpConnection$RequestHandler.content(HttpConnection.java:843)
    at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:647)
    at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:211)
    at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:380)
    at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:395)

回答1:

There might be different causes for that exception, but I ran into it when I implemented my own mapper for RuntimeException. While my mapper was technically correct, it appears to have short-circuited the error handling that's built into Jersey. Removing my mapper cleared up the exception, and I got the expected error codes (405, 415, etc) from Jersey.

Also, make sure you're including a Method for each REST resource. Using your example, that would be:

@Path("/sam")
@POST                             <---- Include a Method (POST, GET, PUT, etc)
@Produces("application/pdf")
@Consumes({"application/xml", "text/xml"})


回答2:

Please, try to specify "Accept" header, like.

headers: {'Content-Type': 'text/xml', 'Accept': 'application/pdf'},