【问题标题】:Origin localhost: is not allowed by Access-Control-Allow-Origin来源 localhost:Access-Control-Allow-Origin 不允许
【发布时间】:2013-06-27 18:34:50
【问题描述】:

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

我的 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 错误警报。 那么我该如何克服这个问题呢?

【问题讨论】:

  • 你在哪里定义了Access-Control-Allow-Origin 标头,值是多少?
  • @rekire 我没有像这个标题那样定义,我不明白我必须在哪里定义它。
  • 你在服务器端使用什么? WCF?
  • @rekire 我没有足够的关于 WCF 的信息。但是我想告诉我的开发环境。我使用 netbeans,java web 项目。以及来自模式的 Restful Web 服务。
  • @rekire 关于客户端,我只使用了html和javascript

标签: jquery ajax rest xmlhttprequest cors


【解决方案1】:

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

在 web.xml 中将其添加到 &lt;servlet&gt;&lt;/servlet&gt; 之间:

<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-02-05
    • 2011-10-27
    • 1970-01-01
    • 2021-10-09
    相关资源
    最近更新 更多