Guru Guru - 6 months ago 14
Java Question

How can I pass a URL from Javascript to Java module

I want to send URL string to a java module whenever a user clicks on chrome extension.

Here's my JS code

document.addEventListener('DOMContentLoaded', function () {

chrome.tabs.query({ currentWindow: true, active: true }, function (tabs) {
alert(tabs[0].url)
var http = new XMLHttpRequest();
var someValue = "http://chums/chummi";

var url = "http://localhost:8080/SalesRESTservice/apicall/restcall2/";

http.open("GET", url, true);

http.setRequestHeader("Content-type", "application/json; charset=utf-8");
http.setRequestHeader("Content-length", params.length);
http.setRequestHeader("Connection", "close");

http.send(someValue);
});
});


Here's my Java

import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;
import org.json.JSONException;
import org.json.JSONObject;

@Path("/restcall2")
public class restcall2 {


@Path("{f}")
@GET
@Produces("text/plain")
public Response RESTcall(@PathParam("f") String f) throws JSONException {
return Response.status(200).entity(f).build();
}

}


Whenever I try to hit URL
http://localhost:8080/SalesRESTservice/apicall/restcall2/

I get no response.
In my network tab it shows
enter image description here

I am using Tomcat server to service the request

Answer

The problem is at your Java side Try changing your java code

  • USE POST
  • Avoid path param & remove @path{f}, this will ensure no other value will not hit URL

    import javax.ws.rs.GET;
    @Path("/restcall2")
    public class restcall2 {      
      @POST
      @Produces("text/plain")
      public Response RESTcall(String f) throws JSONException {
          System.out.println(f);
        return Response.status(200).entity("hello " + f).build();
      }
    

    }