你好我正在为我的家庭作业开发一个项目。首先,我知道有很多关于同一问题的标题,我看了很多,但我无法解决这个问题。如果有人可以帮助我,我会非常愉快。
我的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'
或
'/ShippingDataService/webresources/generic/post'
但它不起作用。我得到了我定义为“POST失败”的ajax错误警报。
那我怎么能克服这个问题呢?
答案 0 :(得分:0)
你好我解决了这个问题:
在web.xml中,将其添加到<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;
}
}