Way to redirect the url from HTML page getting in the response of REST call through AJAX

Problem Statement – I have one CDN server (mylocalmachinedomain:8022) hosted through XAMPP (Apache) on my local machine. In parallel to it, I have written the micro-service (microservicedomainname.com:8444)  to expose it’s REST end point hosted on tomcat server on my local machine. Now First I have to make a REST call (expose end point written in my micro-service) from the HTML page through AJAX. Secondly while exposing the REST call, it would redirect to some other page. So that I need to handle in the HTML page through AJAX. There are 2 ways to do the same –

My micro-service end point java code-

    @GET

    @Path(“/authCall”)

    @ApiOperation(value = “Displays a message”, notes = “The message is read from the properties”, response = Test.class )

    @ApiResponses(value = { @ApiResponse(code = 500, message = “Something went wrong”, response=Test.class) })

    @Produces(MediaType.APPLICATION_JSON)

    public Response authCall() throws Exception {

        logger.info(“This is a log to check oAuth api”);

        //HttpsURLConnection conn = null;

        String url = “https://account.service.com/api/oauth2/authorize?response_type=code&client_id=MY_CLIENT_ID&redirect_uri=MY_REDIRECT_URI&state=MY_STATE;

        URI uri=new URI(url);

        return Response.status(Status.TEMPORARY_REDIRECT).location(uri).header(“Content-Type”, “application/json;charset=UTF-8”).build();

    }

 1st way –

 

  $.ajax({

  type: “GET”,

  url: “https://microservicedomainname.com:8444/myapp/authCall“,

  success: function(data){

  //alert(data);

 document.open();

 document.write(data);

document.close();

   }

 });

2nd way –

var xmlhttp;

    function init() {

       // put more code here in case you are concerned about browsers that do not provide XMLHttpRequest object directly

       xmlhttp = new XMLHttpRequest();

        var url =”https://microservicedomainname.com:8444/myapp/authCall“;

        xmlhttp.open(‘GET’,url,true);

        xmlhttp.send(null);

        xmlhttp.onreadystatechange = function() {

               if (xmlhttp.readyState == 4) {

                  if ( xmlhttp.status == 200) {

                     document.open();

document.write(xmlhttp.responseText);

document.close();

                 }

                 else

                       alert(“Error ->” + xmlhttp.responseText);

              }

        };                           

    }

<body onload=”init()”> 

</body> 

Hope it helps 🙂

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s