如何在Ajax中设置标题?

我对POST方法进行了Ajax调用,它使用XML并生成PDF。但是如何在Ajax中设置标题?我已经浏览过很多相关的帖子,但是没有发现太多有用的信息。

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拥有XML数据。

Rest :

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

我一直得到这个结果:

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)

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"})

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

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