3

I am trying to call a servlet using ajax call as below:

$.ajax({
  url: 'CheckingAjax',
  type: 'GET',
  data: { field1: "hello", field2 : "hello2"} ,
  contentType: 'application/json; charset=utf-8',
  success: function (response) {
    //your success code
    alert("success");
  },
  error: function (errorThrown) {
    //your error code
    alert("not success :"+errorThrown);
  }                         
});

However, it goes to error function and shows alert:

not success :Not Found

How is this caused and how can I solve it?

8
  • What is the error code? Does the request reach the server? What do the browser developer tools (network tab) show? Commented May 12, 2015 at 4:55
  • @Thilo error throws "Not found" and request does not reach server. Commented May 12, 2015 at 5:03
  • Are you sure the URL is right? What happens when you type the URL directly into the address bar (or curl it)? Commented May 12, 2015 at 5:12
  • @Thilo localhost:8080/FullcalendarProject/CheckingAjax this is my servlet url in address bar. its working in address bar Commented May 12, 2015 at 5:15
  • and what is the url before you make the ajax call? I just want to see if the relative url used is correct Commented May 12, 2015 at 6:47

1 Answer 1

7

When you specify a relative URL (an URL not starting with scheme or /), then it will become relative to the current request URL (the URL you see in browser's address bar).

You told that your servlet is available at:

http://localhost:8080/FullcalendarProject/CheckingAjax

Imagine that the web page where your ajax script runs is opened via:

http://localhost:8080/FullcalendarProject/pages/some.jsp

And you specify the relative URL url: "CheckingAjax", then it will be interpreted as:

http://localhost:8080/FullcalendarProject/pages/CheckingAjax

But this does not exist. It will thus return a HTTP 404 "Page Not Found" error.

In order to get it to work, you basically need to specify the URL using one of below ways:

  1. url: "http://localhost:8080/FullcalendarProject/CheckingAjax"

    This is not portable. You'd need to edit it everytime you move the webapp to another domain. You can't control this from inside the webapp.

  2. url: "/FullcalendarProject/CheckingAjax"

    This is also not really portable. You'd need to edit it everytime you change the context path. You can't control this from inside the webapp.

  3. url: "../CheckingAjax"

    This is actually also not portable, although you can fully control this from inside the webapp. You'd need to edit it everytime you move around JSP into another folder, but if you're moving around JSPs you're basically already busy coding, so this could easily be done at the same time.

  4. Best way would be to let JSP EL dynamically print the current request context path. Assuming that the JS code is enclosed in JSP file:

    url: "${pageContext.request.contextPath}/CheckingAjax"

    Or when it's enclosed in its own JS file (good practice!), then create either a global JS variable:

    <script>var contextPath = "${pageContext.request.contextPath}";</script>
    <script src="yourajax.js"></script>
    

    With

    url: contextPath + "/CheckingAjax"

    or a HTML5 data attribute on the document element:

    <html data-contextPath="${pageContext.request.contextPath}">
        <head>
            <script src="yourajax.js"></script>
    

    With

    url: $("html").data("contextPath") + "/CheckingAjax"

Sign up to request clarification or add additional context in comments.

1 Comment

Why would this not work after going through mod_proxy in apache ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.