Quantcast
Channel: GUJ - Tópicos com a tag java
Viewing all articles
Browse latest Browse all 17410

[RESOLVIDO] Erro(CORS) ao executar chamada no webservice com Jetty

$
0
0

@echos escreveu:

Boa noite, estou tentando realizar uma chamada a um webservice más está retornando o erro abaixo;

Requisição cross-origin bloqueada: A política de mesma origem (Same Origin Policy) impede a leitura do recurso remoto em http://localhost:8484/testeImpressao/impressao/. (Motivo: o cabeçalho CORS 'Access-Control-Allow-Origin' não está presente).

Segue o código que estou utilizando.

Servidor WebService

@Path("testeWS")
public class ApplicationCommandResource {
      
        
    @POST
    @Produces(MediaType.APPLICATION_JSON)
    @Path("/teste")
    @Consumes(MediaType.APPLICATION_JSON)
    public Response impressao( @QueryParam("nome") String stringJson) {        
               
        JSONObject jsonObject = new JSONObject();
        
        Integer tam = 0;
        jsonObject.put(tam.toString(), "teste 1");        
        tam += 1;
        jsonObject.put(tam.toString(), "teste 2");
        tam += 1;
        jsonObject.put(tam.toString(), "teste 3");   
        
        return Response
            .status(200)
            .header("Access-Control-Allow-Origin", "*")
            .header("Access-Control-Allow-Headers", "origin, content-type, accept, authorization")
            .header("Access-Control-Allow-Credentials", "true")
            .header("Access-Control-Allow-Methods", "POST")            
            .entity(jsonObject.toString())
            .build();         
                  
    }`

CLiente WebService

$.ajax({
                    type: "POST",
                    url: "http://localhost:8484/testeWS/teste/" ,
                    data: "{'0':'1','1':'teste','2':'1','3':'asw'}",				       	
                    dataType: "json",  
                    headers: { 'Content-Type': 'application/json' ,
                        'X-Requested-With': 'XMLHttpRequest', 
                        'Access-Control-Allow-Origin': '*' ,
                        'Access-Control-Allow-Headers':'origin, content-type, accept, authorization'
                    },  	
                    success: function(data){  				         
                        alert("saida:" + data[3]); 	
                    },beforeSend: function(){				       		
                        $("body").addClass("loading"); 
                    },
                    complete: function(){					    	
                        $("body").removeClass("loading");
                    },
                    error: function (xhr, status, error) {
                        
                     
                    }
                });

Mensagens: 5

Participantes: 3

Ler tópico completo


Viewing all articles
Browse latest Browse all 17410

Latest Images

Trending Articles

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>