Lil
Lil

Reputation: 25

jquery ajax returns element not found

I have a REStful webservice (java, jersey) to do some stuff. every function that calls an ajax request (GET or POST) with an url to the REST controller works fine... except of the recent ones and I do not know why.. i tried everything and stuck with this problem for nearly three days (wrote 3 different functions, changed from GET to POST, rewrote the function with new pathannotiation, tried to call on pageload.. renamed everything), I realy appreciate ANYTHING that could help me...

if the url contains rest/* the controller forwards it to the class which implements the needed functions..

JS function

function testFunc() {
    $.ajax({
        url: "rest/name/wut",
        type: "GET",
        contentType: "application/json;charset=utf-8",
        success: function(response) {
            alert("LSKDFJLSDKJFLKSD " + response);
        },
        error: function(response) {
            alert("ma oidaaaa " + JSON.stringify(response));
        }

    });
};

Java Code in the RESTClass...

    @GET
    @Path("/wut")
    @Produces(MediaType.APPLICATION_JSON)
    private String wut() {
        JSONObject json = new JSONObject();
        json.put("print", "wuut");
        return json.toString();
    }

It does not matter if the method is doing anything useful or not... it just returns 404 no element found. (it is not even called) Therefore i tried different new methods in the RESTClass... like:

@GET
@Path("/wut")
@Produces(MediaType.APPLICATION_JSON)
private String wut() throws IOException {
    URL url = new URL(url);
    StringBuffer response = new StringBuffer();
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
     connection.setRequestMethod("GET");

    BufferedReader in = new BufferedReader(new InputStreamReader(
            connection.getInputStream()));
    String inputLine;

    while ((inputLine = in.readLine()) != null) {
        response.append(inputLine);
    }
    in.close();
    return response.toString();
}

I am using this snipped in another method, which is working.. I replaced the real URL with "url" for posting btw.

I also tried different MediaTypes: WILDCARD, TEXT/PLAIN... And to just return a String...

Anyone any ideas (and SORRY for the bad english, I am really desperate so i did not do a spellcheck and english is not my native :( )

Upvotes: 0

Views: 342

Answers (2)

bernland
bernland

Reputation: 688

Two ideas:

  • First, declare your wut() method as public: public String wut()
  • Second, try to call your method in a browser, for example http://localhost/rest/name/wut and see what happens

Upvotes: 1

Peter Paul Kiefer
Peter Paul Kiefer

Reputation: 2134

I would try to use an absolute path: Change:

url: "rest/name/wut",

to

url: "/rest/name/wut",

The error message tells me that, your client did not try the address the server provides.

Upvotes: 0

Related Questions