0

您好,我正在为我的家庭作业开发一个项目。首先,我知道有很多关于同一个问题的标题,我看了很多,但我无法解决这个问题。如果有人可以帮助我,我会非常高兴。

我的ajax调用是:

function submit(name,surname,source,destination,distance,volume,weight,price){

var xml_string = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'+ "<shippingData><FIRSTNAME>" + name + "</FIRSTNAME>"+
                    "<LASTNAME>" + surname + "</LASTNAME>"+
                    "<SOURCECITY>" + source + "</SOURCECITY>"+
                    "<DESTINATIONCITY>" + destination + "</DESTINATIONCITY>"+
                    "<DISTANCE>" + distance + "</DISTANCE>"+
                    "<VOLUME>" + volume + "</VOLUME>"+
                    "<WEIGHT>" + weight + "</WEIGHT>"+
                    "<PRICE>" + price + "</PRICE>"+
                    "</shippingData>";





$.ajax({
        url: 'http://localhost:8084/ShippingDataService/webresources/generic/post',
        type: 'POST',
        data: xml_string,
        dataType: 'xml',
        contentType: 'application/xml',
        success: function (data) {

            alert("OK");

        },
        error: function (responseData, textStatus, errorThrown) {
            alert('POST failed.');
        }
    }); 

}

根据我的作业规则,我必须使用 XML 来传输数据。我的服务器端是这样的:

@POST
@Consumes(MediaType.APPLICATION_XML)
@Produces(MediaType.APPLICATION_XML)
@Path("post")
public ShippingData postXML(ShippingData content) {
    System.out.println("POST");

    return content;
}

Shippingdata 类是:

package ship;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;


@XmlRootElement
public class ShippingData {

    public int SID;

    @XmlElement (name = "FIRSTNAME")public String firstName;

    @XmlElement (name = "LASTNAME")public String lastName;

    @XmlElement (name = "SOURCECITY")public String sourceCity;

    @XmlElement (name = "DESTINATIONCITY")public String destinationCity;

    @XmlElement (name = "DISTANCE")public int distance;

    @XmlElement (name = "VOLUME")public int volume;

    @XmlElement (name = "WEIGHT")public int weight;

    @XmlElement (name = "PRICE")public int price;

    public ShippingData() {

    }

    public ShippingData(int SID, String firstName, String lastName, String sourceCity, String destinationCity, int distance, int volume, int weight, int price) {
        this.SID = SID;
        this.firstName = firstName;
        this.lastName = lastName;
        this.sourceCity = sourceCity;
        this.destinationCity = destinationCity;
        this.distance = distance;
        this.volume = volume;
        this.weight = weight;
        this.price = price;
    }



}

我也尝试将 ajax 调用 url 更改为 'localhost:8084/ShippingDataService/webresources/generic/post'or '/ShippingDataService/webresources/generic/post'但它不起作用。我收到了定义为“POST 失败”的 ajax 错误警报。那么我该如何克服这个问题呢?

4

1 回答 1

0

您好,我解决了这样的问题:

在 web.xml 中将其添加到 between <servlet></servlet>

<init-param>
        <param-name>com.sun.jersey.spi.container.ContainerResponseFilters</param-name>
        <param-value>"YOURPACKAGENAME".ResponseCorsFilter</param-value>
</init-param>

并将此类添加到您的包中

import com.sun.jersey.spi.container.ContainerRequest;
import com.sun.jersey.spi.container.ContainerResponse;
import com.sun.jersey.spi.container.ContainerResponseFilter;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;

public class ResponseCorsFilter implements ContainerResponseFilter {

@Override
public ContainerResponse filter(ContainerRequest req, ContainerResponse contResp) {

        ResponseBuilder resp = Response.fromResponse(contResp.getResponse());
        resp.header("Access-Control-Allow-Origin", "*")
                .header("Access-Control-Allow-Methods", "GET, POST, OPTIONS");

        String reqHead = req.getHeaderValue("Access-Control-Request-Headers");

        if(null != reqHead && !reqHead.equals("")){
            resp.header("Access-Control-Allow-Headers", reqHead);
        }

        contResp.setResponse(resp.build());
            return contResp;
    }
}
于 2013-06-30T17:39:48.697 回答