简体   繁体   English

原始本地主机:Access-Control-Allow-Origin不允许

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

Hello I am developing a project for my homework. 您好,我正在为我的作业开发一个项目。 First of all, I know there are many title about same problem, I looked at many of them, but I couldn't resolve this issue. 首先,我知道关于同一问题的很多书名,我看了很多,但是我无法解决这个问题。 If someone could help me, I will be very pleasured. 如果有人可以帮助我,我将非常高兴。

My ajax call is: 我的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.');
        }
    }); 

} }

I have to use XML to transfer data, according to rule of my homework. 根据我的作业规则,我必须使用XML来传输数据。 My server side is like that: 我的服务器端是这样的:

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

    return content;
}

Shippingdata class is: 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;
    }



}

I also tried to change ajax call url to 'localhost:8084/ShippingDataService/webresources/generic/post' or '/ShippingDataService/webresources/generic/post' but it doesn't work. 我也尝试将ajax调用URL更改为'localhost:8084/ShippingDataService/webresources/generic/post''/ShippingDataService/webresources/generic/post'但它不起作用。 I get the ajax error alert that I defined as "POST failed". 我收到我定义为“ POST失败”的ajax错误警报。 So how can I overcome this problem? 那么我该如何克服这个问题呢?

Hello I solved the problem like that: 您好,我解决了这样的问题:

In the web.xml add this to between <servlet></servlet> : 在web.xml中,将其添加到<servlet></servlet>

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

And add this class to your package 并将此类添加到您的包中

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.

相关问题 Access-Control-Allow-Origin不允许来源http:// localhost:8080 - Origin http://localhost:8080 is not allowed by Access-Control-Allow-Origin Access-Control-Allow-Origin不允许使用origin http:// localhost:1716 - Origin http://localhost:1716 is not allowed by Access-Control-Allow-Origin Ajax-&#39;Origin localhost不允许Access-Control-Allow-Origin&#39; - Ajax - 'Origin localhost is not allowed by Access-Control-Allow-Origin' Access-Control-Allow-Origin 不允许来源 - Origin is not allowed by Access-Control-Allow-Origin Access-Control-Allow-Origin不允许使用的URL - URL not allowed by Access-Control-Allow-Origin Access-Control-Allow-Origin - localhost - Access-Control-Allow-Origin - localhost 本地主机上没有“ Access-Control-Allow-Origin” - No 'Access-Control-Allow-Origin' on the localhost XMLHttpRequest无法加载http:// localhost:8089 / jquery。 Access-Control-Allow-Origin不允许使用原点null - XMLHttpRequest cannot load http://localhost:8089/jquery. Origin null is not allowed by Access-Control-Allow-Origin XMLHTTPRequest无法加载http:// ...原始http:// localhost:Access-Control-Allow-Origin不允许使用端口 - XMLHTTPRequest cannot load http://… Origin http://localhost:port is not allowed by Access-Control-Allow-Origin XHR错误:原始http://本地主机不允许使用Access-Control-Allow-Origin - XHR Error : Origin http://localhost is not allowed by Access-Control-Allow-Origin
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM