“La croix-Origine de la Demande Bloqué: de La Même Origine que la Politique” d'Erreur dans le navigateur

J'ai eu ce message d'erreur quand j'essaie de POSTER fichier json à mon serveur.

Sur mon serveur, le code est:

    @POST
    @Path("updatedata")
    @Produces("text/plain")
    @Consumes("application/json")
    public Response UpdateData(String info) {
        Gson gson = new Gson();
        List<Data> list = gson.fromJson(info, new TypeToken<List<Data>>() {
        }.getType());

        int is_success = 0;
        try {
            is_success += trainingdata.updateData(list);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        String returnjson = "{\"raw\":\"" + list.size() + "\",\"success\":\"" + is_success + "\"}";
        return Response.ok().entity(returnjson).header("Access-Control-Allow-Origin", "*").header("Access-Control-Allow-Methods", "POST").build();
    }

Je peux mettre à jour mes données avec succès à travers RESTClient - un Plugin Chrome.

Mais quand je construis le frontend et d'essayer d'appeler l'API par le biais de jaascript,
Firefox affiche: Cross-Origin Demande Bloqué: La Même Origine ....
Chrome affiche: XMLHttpRequest ne peut pas charger ... Pas de "Access-Control-Allow-Origin' en-tête est présent sur la ressource demandée. Origine '...' est donc pas autorisé à accéder

J'ai écrit le javascript comme ceci:

var json = JSON.stringify(array);

var xhr = new XMLHttpRequest();
xhr.open("POST", "http://myurl:4080/updatedata", true);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.send(json);

xhr.onload = function (e) {
    if (xhr.readyState === 4) {
        if (xhr.status === 200) {
            alert('hello');
        }
    }
};
xhr.onerror = function (e) {
    console.error(xhr.statusText);
};

Est-il un problème avec mon code javascript?

Je déploie mon backend code et avant la fin de code dans la même machine.

La fonction GET fonctionne correctement.

@GET
@Produces("application/json")
@Path("/{cat_id}")
public Response getAllDataById(@PathParam("cat_id") String cat_id) {
    ReviewedFormat result = null;
    try {
        result = trainingdata.getAllDataById(cat_id);
        Gson gson = new Gson();
        Type dataListType = new TypeToken<ReviewedFormat>() {
        }.getType();
        String jsonString = gson.toJson(result, dataListType);
        return Response.ok().entity(jsonString).header("Access-Control-Allow-Origin", "*").header("Access-Control-Allow-Methods", "GET").build();

    } catch (SQLException e) {
        logger.warn(e.getMessage());
    }
    return null;
}

Frontale:

var xhr = new XMLHttpRequest();
xhr.open("GET", "http://URL:4080/mywebservice/v1/trainingdata/" + cat_id, true);

xhr.onload = function (e) {
    if (xhr.readyState === 4) {
        if (xhr.status === 200) {
            //console.log(xhr.responseText);
            var jsoninfo = xhr.responseText;
            var obj = JSON.parse(jsoninfo);
        }
     }
}
les protocoles, les domaines et les ports doivent correspondre
developer.mozilla.org/en-US/docs/Web/Security/...

OriginalL'auteur Freya Ren | 2014-11-12