簡體   English   中英

原始本地主機:Access-Control-Allow-Origin不允許

[英]Origin localhost: is not allowed by Access-Control-Allow-Origin

您好,我正在為我的作業開發一個項目。 首先,我知道關於同一問題的很多書名,我看了很多,但是我無法解決這個問題。 如果有人可以幫助我,我將非常高興。

我的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錯誤警報。 那么我該如何克服這個問題呢?

您好,我解決了這樣的問題:

在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;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM