Ir al contenido principal

[GWT] XMLHttpRequest cannot load. Origin is not allowed by Access-Control-Allow-Origin

I am developing an app front end  with GWT and this connect to other app that publish AppEngine Endpoints services. 
(Estoy desarrollando una aplicación  front end con GWT que se conecta a otra app que publica servicios rest en AppEngine usando Google Cloud EndPoints).

But, I is generating some problems (is not allowed by Access-Control-Allow-Origin.): 
(Pero, esto  me esta generando  algunos problemas).

The app GWT  call endpoint using  RequestBuilder. and the Endpoint app is other app with other domain. and both was deployed in https://appengine.google.com
(La app GWT llama a los servicios rest' endpoints' usando RequestBuilder. y la app endpoint es otra app con otro dominio. Ambas fueron deployadas en https://appengine.google.com).

Domains of the Apps:
-xappgwtx.appspot.com (GWT).
-xappendpointsx.appspot.com (Google Cloud Endpoints).

GWT Code:

public String getDevice(String id) {
String url = URL_SERVICE_BASE + "deviceendpoint/v1/device/get/" + id;
RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, url);
logger.info("::::::::::::sendRequest:::::::");
try {
builder.sendRequest(null, new RequestCallback() {
public void onResponseReceived(Request request, Response response) {
logger.info("response=" + response.getText() + " request:" + request);
resp = response.getText();
if (response.getStatusCode() == 200) {
System.out.println("response=" + response.getText());
logger.info("response=" + response.getText());
}
}

public void onError(Request request, Throwable exception) {
logger.info("onError response=" + exception.getMessage());
resp = exception.getMessage();
exception.printStackTrace();
}
});
} catch (RequestException e) {
logger.info("exception=" + e.getMessage());
e.printStackTrace();

}
return resp;
}

Chrome Console:

After much research in lists, stackoverflow and other forum, solve the problem
(Luego  de mucha investigación en listas y stackoverflow llegue a solucionar el  problema)

Solution: to solve this issue simply assign a variable to your json string into the eval

* Before:

  public static native JavaScriptObject fromJson(String json) /*-{

    return eval("(" + json + ")");

  }-*/;
  
  public final String toJson() {
    return new JSONObject(this).toString();
  }
  
  protected BaseJso() {
  }

* After: 

public class BaseJso extends JavaScriptObject {



public static native JavaScriptObject fromJson(String json) /*-{
eval('var res = ' + json);
return res;
}-*/;

public final String toJson() {
return new JSONObject(this).toString();
}

protected BaseJso() {
}

}

References:


Comentarios

Entradas populares de este blog

Primer CodeLab de Web Services con Mule ESB, JAX WS, SOAP, WSDL, Spring

Primer CodeLab de Web Services usando [ Mule ESB , JAX WS , SOAP , WSDL , Spring ]. en este post no hablaré nada teórico sobre Mule , ya que ello lo pueden encontrar Googleando, será un lab totalmente práctico. Requisitos: - JDK  1.6 - MuleStudio - soapUI Paso a Paso para crear Web Services con Mule : 1. Crear proyecto Mule ( MuleStudio ):  Next > Next > Next > Finish 2.- Crear el Objeto Producto.java package com.dmotta.mule.labuno.mulelabuno.bo; import java.io.Serializable; public class Producto implements Serializable { private String id; private String nombre; private String marca; private String descripcion; public Producto() { } public Producto(String id, String nombre, String marca, String descripcion) { this.id=id; this.nombre=nombre; this.marca=marca; this.descripcion=descripcion; } //getters/setters } 3.- Crear la Interface que publicará los métodos listarProductos() y getDetalleProdu...

RESTful Webservices con Java (Jersey / JAX-RS) - Tutorial Uno

REST con Java (JAX-RS) usando Jersey Este articulo explica como desarrollar RESTful web services en Java con JAX-RS implementacion de Jersey. En este ejemplo se usara SpringSource Tools Suite(Eclipse Helios), Java 1.6, SpringSource TC Server and JAX-RS 1.1. (Jersey 1.4). 1. REST - Representational State Transfer 1.1 Informacion General: En una Arquitectura REST tipicamente se tiene un REST server , el cual provee acceso a los Clientes REST  que acceden a consultar, modificar un Recurso REST . REST permite que los recursos sean presentados en diferentes tipos de datos, "text, html, xml, JSON" .  El Cliente REST  puede generar una peticion de un tipo de dato via el protocolo HTTP. 1.2. HTTP metodos Los metodos estandar HTTP usados en en REST son  PUT, GET, POST, DELETE. 1.3. RESTFul webservices Un RESTFul webservices es basados en metodos HTTP y en el concepto REST.  Por lo general se define URI base para los servicios, los MIME-types sus...