0

I'm working on a project whereby in a JSP page I make an AJAX GET request to URL:

http://[domain]/ChatEngine/ChatServlet/users/login?roomId=0&name=Alan

This is pointing to a servlet I created named ChatServlet with my web.xml looking like this:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<display-name>ChatEngine</display-name>
<servlet> 
   <servlet-name>ChatEngine</servlet-name> 
   <servlet-class>com.tacticalenterprisesltd.chat.servlets.ChatServlet</servlet-class> 
</servlet>

<servlet-mapping> 
 <servlet-name>ChatEngine</servlet-name> 
 <url-pattern>/ChatServlet/*</url-pattern> 
</servlet-mapping> 

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>    
</welcome-file-list>
</web-app>

My index.jsp jquery AJAX code looks like this:

    var chaturl = "http://[domain]/ChatEngine/ChatServlet";
    var dataString = "roomId=" + roomID + "&name=" + uname;     
    $.ajax({
      type: "GET",
      url: chaturl + "/users/login", 
      data: dataString,
      contentType: "application/json; charset=utf-8",
      dataType: "json",
      cache: false,
      statusCode: {
          404: function() {
              console.log('404 Could not contact server.');
          },
          500: function() {
              console.log('500 A server-side error has occurred.');
          }
      },
      success: function(msg) {
          ...               
      },
      error: function(err) {
        alert('Login Error: ' + err.responseText + '  Status: ' + err.status + ' Response Status: ' + err.responseStatus); 
      }
    });

When I execute the AJAX request it keeps going immediately to the error property and I can't figure out why.

Here's the firebug readout: enter image description here Notice how it says, "OPTIONS" instead of "GET". I suspect that's a clue, but I don't understand why the browser is interpreting the request this way when I explicitly set the type attribute to "GET".

In the servlet, I wrote some println statements, but they aren't even being printed to the Eclipse console, so I know the servlet isn't even being called.

I know the URL is correct because I opened a new tab in FireFox, entered the URL into the address field, and immediately got a correct response from the servlet. So there's something screwy with the AJAX code. At this point I can't see the forest for the trees.

Please advise.

Alan

Alan
  • 822
  • 1
  • 16
  • 39

1 Answers1

0

You could be making a CORS request by accident because you are, for example, making an AJAX request from localhost to 127.0.0.1. Even though they end up on the same server, they have separate cookies and are considered by the browser to be different servers.

If that is the problem, you should change your ajax call to

var chaturl = "/[domain]/ChatEngine/ChatServlet";

So that it will use whatever URL you are currently pointing to.

If you do need the Ajax server to be on a different server, you need a cross site request (CORS) ? You have to configure the server to allow you to connect to it.

For tomcat, you can use

<filter>
  <filter-name>CorsFilter</filter-name>
  <filter-class>org.apache.catalina.filters.CorsFilter</filter-class>
</filter>
<filter-mapping>
  <filter-name>CorsFilter</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>

The browser sends an OPTIONS preflight request before making the real request, but it's failing because your server is not configured

See http://enable-cors.org/index.html

Ruan Mendes
  • 90,375
  • 31
  • 153
  • 217
  • I wasn't even aware of this CORS thing. So the answer to your question is NO. – Alan Mar 14 '14 at 21:39
  • Are you sure? Your JavaScript looks like it's setting a URL with an absolute server name. I have never heard of OPTIONS except in the CORS context... See this question http://stackoverflow.com/questions/1256593/jquery-why-am-i-getting-an-options-request-instead-of-a-get-request – Ruan Mendes Mar 14 '14 at 21:46
  • This CORS stuff is way too deep for me. I don't understand any of it. All I know is, the URL is correctly pointing to the servlet, therefore, the AJAX request should work. Does this have something to do with the fact that I'm using my own computers IP address or "localhost" for the domain? I've not come across this before. For normal development, these have always worked before without any hickups. – Alan Mar 14 '14 at 21:55
  • 1
    Yes Alan, if the server on the browser's URL box and the request you're sending it to aren't exactly the same, the browser will try to make an CORS request. In that case, just make sure you don't use an absolute path (http://server/users/login), use a server relative path(/users/loing) so that it can work whether you type the server name, localhost or 127.0.0.1 – Ruan Mendes Mar 14 '14 at 22:04
  • OK. I think I'm starting to see the light... Because I explicitly provided an absolute path, the browser thinks I'm trying to get resources somewhere other than the Tomcat server running on my own computer. By changing the path to a relative one as you suggested made the difference. I changed the url attribute to: url: "/ChatEngine/ChatServlet/users/login". Finally, that worked. At least I learned something new today. Thanks. – Alan Mar 14 '14 at 22:20
  • @Alan Did this not answer your question>? Please accept the answer. – Ruan Mendes Mar 17 '14 at 16:56