首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Origin localhost: Access-Control-Allow-Origin不允许

Origin localhost: Access-Control-Allow-Origin不允许
EN

Stack Overflow用户
提问于 2013-06-30 21:38:31
回答 1查看 512关注 0票数 0

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

我的ajax调用是:

代码语言:javascript
运行
复制
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来传输数据。我的服务器端是这样的:

代码语言:javascript
运行
复制
@POST
@Consumes(MediaType.APPLICATION_XML)
@Produces(MediaType.APPLICATION_XML)
@Path("post")
public ShippingData postXML(ShippingData content) {
    System.out.println("POST");

    return content;
}

Shippingdata类为:

代码语言:javascript
运行
复制
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 call url改为'localhost:8084/ShippingDataService/webresources/generic/post''/ShippingDataService/webresources/generic/post',但不起作用。我收到了ajax错误警报,我将其定义为"POST failed“。那么我该如何克服这个问题呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-07-01 01:39:49

你好,我是这样解决问题的:

在web.xml中,将以下内容添加到<servlet></servlet>之间:

代码语言:javascript
运行
复制
<init-param>
        <param-name>com.sun.jersey.spi.container.ContainerResponseFilters</param-name>
        <param-value>"YOURPACKAGENAME".ResponseCorsFilter</param-value>
</init-param>

并将这个类添加到包中

代码语言:javascript
运行
复制
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;
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17390918

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档