1

While trying HTTP headers which had i set on server unable to access back on java script.below is my code.Kindly some help me to resolve this.

Jersey :

@Path("/redirect")
public class RedirectDemo {

@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response getRedirect(@Context ServletContext context,UserTO user) {
    UriBuilder builder = UriBuilder.fromPath(context.getContextPath());
    System.out.println("User name is:"+user.getUserName());
    System.out.println("Password is:"+user.getPassword());
    builder.path("/main.html");
    return Response
            .status(Response.Status.SEE_OTHER)
            .header(HttpHeaders.AUTHORIZATION,"Response authorize")
            .header("Access-Control-Allow-Origin", "*")
            .header("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT")
            .header("Access-Control-Expose-Headers",HttpHeaders.AUTHORIZATION)
            .header(HttpHeaders.LOCATION, builder.build())
            .build();

    }
  }

Java script :

        <body>
    <div class="container-test">
    <h3>
        <strong>iDNS</strong>
    </h3>
   </div>
    <div class="container">
    <form class="form-signin" id="loginForm" name="loginForm"
        action="/JerseyPageRedirection/redirect/redirect" method="post" id="login_form">
        <div class="errmsg text-center"></div>
        <label for="inputEmail">Email address</label> <input type="email"
            id="emailId" class="form-control" name="emailId"
            placeholder="Email address" required autofocus> <br> <label
            for="inputPassword">Password</label> <input type="password"
            id="password" class="form-control" name="password"
            placeholder="Password" required> <br>
        <!-- id="login-submit" -->
        <button type="button" class="btn btn-lg btn-primary btn-block"
            onclick="doLogin()">Sign in</button>
    </form>
</div>
<script>
    function doLogin() {
        var userName = window.btoa(document.getElementById('emailId').value);
        var password = window.btoa(document.getElementById('password').value);
        var formData = JSON.stringify({userName:userName,password:password});
        var xhr = new XMLHttpRequest();  
        xhr.open("POST", "/JerseyPageRedirection/redirect/redirect");  
        xhr.setRequestHeader("Content-type", "application/json");
        xhr.send(formData); 
        xhr.onreadystatechange = function() {
            if (xhr.readyState == 4)
                  if (xhr.status == 200)
                    var json_data = xhr.responseText;
                    window.location.href = xhr.responseURL;
        }
    }
        </script>
    </body>

Thanks ,i need to read header values on java script.

Murugesan Era
  • 335
  • 1
  • 17

1 Answers1

0

See: Accessing the web page's HTTP Headers in JavaScript

This article describes the methods to obtain String representations of all the headers, and a value for a named header: https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest

DOMString getAllResponseHeaders();
DOMString? getResponseHeader(DOMString header);
Community
  • 1
  • 1
Andrew Regan
  • 5,087
  • 6
  • 37
  • 73