title: Json Web Token - jose4j - SyntaxError: Unexpected token e in JSON at position 0
body: <p>I have a controller which tries to get a token.</p>
  
  <p>I got this error in postman when I execute it in the view PRETTY </p>
  
  <blockquote>
    <p>Unexpected 'e'</p>
  </blockquote>
  
  <p>But if I go to the view RAW I can see the token like this.</p>
  
  <pre><code>eyJraWQiOiIxIiwiYWxnIjoiUlMyNTYifQ.eyJpc3MiOiJJc3N1ZXIiLCJhdWQiOiJBdWRpZW5jZSIsImV4cCI6MTQ3NTQ1OTMyNiwianRpIjoiTmF3d000bDVGRmFRZ0dBQkwzS3N5USIsImlhdCI6MTQ3NTQ1ODcyNiwibmJmIjoxNDc1NDU4NjA2LCJzdWIiOiJzdWJqZWN0IiwiZW1haWwiOiJtYWlsQGV4YW1wbGUuY29tIn0.f97SFDaAjUyUDK_UQgwgnCTewd0yw6tWK6DFLzpALFq177f1QMTYPbVdiIG1ViJ0FNJ6fUCleCd8BmrToUn25VSmRv799dtcz-xaN1kOgw90NQ00kPUhnDXG01-7hImkHfbmZZWORukP2yPK1sHWzpdjg9fJOvRZpZ6ZWli4HeuYRJqsFOv7PvwmGH9JnfRTf_2tboL-oAYBpT367eh60TggrvMgmrO_Taj5M7qGG0GpbwuVh_HTAkaKv7T2WmuZ2JPANhe5JvY_DDaqChtwd0IPREAhK3Xr-nTOIuwbQ0Y1hhOGfvDmikQj6DXnCERYixP6eR1dhC8n3bKvXyaVmA
  </code></pre>
  
  <p>This is the code of my controller.</p>
  
  <pre><code>@Path("/demo")
  @GET
  @Produces(MediaType.APPLICATION_JSON)
  public Response testMethod() throws JSONException, IOException {
      RsaJsonWebKey rsaJsonWebKey = RsaJwkGenerator.generateJwk(2048);
      rsaJsonWebKey.setKeyId("k1");
      JwtClaims claims = new JwtClaims();
      claims.setIssuer("Issuer");  
      claims.setAudience("Audience");
      claims.setExpirationTimeMinutesInTheFuture(10);    
      claims.setGeneratedJwtId(); 
      claims.setIssuedAtToNow();
      claims.setNotBeforeMinutesInThePast(2); 
      claims.setSubject("subject"); 
      claims.setClaim("email","mail@example.com"); 
      JsonWebSignature jws = new JsonWebSignature();
  
      jws.setPayload(claims.toJson());
      jws.setKey(rsaJsonWebKey.getPrivateKey());
      jws.setKeyIdHeaderValue(rsaJsonWebKey.getKeyId());
  
      jws.setAlgorithmHeaderValue(AlgorithmIdentifiers.RSA_USING_SHA256);
  
      String jwt = jws.getCompactSerialization();
      if(jwt == null){
          return Response.status(204).entity(jwt).build();
      }
      return Response.status(200).entity(jwt).build();
  }
  </code></pre>
  
  <p>I ignore the error in postman but I get the same error when try to execute it in Chrome.</p>
  
  <p>I try to call my RESTful controller with angular like this, but I always get into the onError method with the message within the response parameter.</p>
  
  <pre><code>angular.min.js:118 SyntaxError: Unexpected token e in JSON at position 0
  </code></pre>
  
  <p>This is the code in angular</p>
  
  <pre><code>app.service('TokenService', function($http){
      this.getToken = function(){
        function onSuccess(response){
            console.log('got it');
        }
  
        function onError(response){
            console.log('fail');
        }
  
        return $http({
            method : 'GET',
            url : 'http:localhost:8080/rest/demo',
            header: {'Content-Type' : 'application/json'}
        }).then(onSuccess, onError);
      }
  }
  </code></pre>
  
  <p>My reference of the code for the token is from <a href="https://bitbucket.org/b_c/jose4j/wiki/JWT%20Examples" rel="nofollow">here</a> with Jose4j</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I solved this. I still think the way I did it initially it should work also, but I don't still understand why I get the error.</p>
  
  <p>I created a pojo named Token with a property token as String then I changed this</p>
  
  <pre><code>return Response.status(200).entity(jwt).build();
  </code></pre>
  
  <p>to this:</p>
  
  <pre><code>Token token = new Token();
  token.setToken(jwt);
  return Response.status(200).entity(token).build();
  </code></pre>
  
  <p>This is my workaround to return a real json object.</p>