1

so i have this problem, i have a Java Back-End which is basically restful services + db layer, which answers queries from the Javascript front end.

I have a javascript ajax request that works perfectly in Chrome, but doesn't even get to the server when i use Firefox/IE.

AJAX ERROR is : readyState=0, status=0, statusText="error"

Here is the web.xml

<?xml version="1.0" encoding="UTF-8"?>
<!-- This web.xml file is not required when using Servlet 3.0 container,
     see implementation details http://jersey.java.net/nonav/documentation/latest/jax-rs.html -->
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <servlet>
        <servlet-name>Jersey Web Application</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>jersey.config.server.provider.packages</param-name>
            <param-value>com.library.ws</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>


    <servlet-mapping>
        <servlet-name>Jersey Web Application</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>WEB-INF/spring_config.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <filter>
        <filter-name>CORS</filter-name>
        <filter-class>com.thetransactioncompany.cors.CORSFilter</filter-class>
        <init-param>
            <param-name>cors.allowOrigin</param-name>
            <param-value>*</param-value>
        </init-param>
        <init-param>
            <param-name>cors.supportedMethods</param-name>
            <param-value>GET, POST, HEAD, PUT, DELETE</param-value>
        </init-param>
        <init-param>
            <param-name>cors.supportedHeaders</param-name>
            <param-value>Accept, Origin, X-Requested-With, Content-Type,
                Last-Modified</param-value>
        </init-param>
        <init-param>
            <param-name>cors.exposedHeaders</param-name>
            <param-value>Set-Cookie</param-value>
        </init-param>
        <init-param>
            <param-name>cors.supportsCredentials</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>CORS</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

ajax javascript call

$(document).on('click', '.btn-login', function (event) {
    event.preventDefault();
    var holdUserName = document.getElementById("username1").value;
    var holdPass = document.getElementById("password1").value;
    Cookies.set('username', holdUserName, {expires: 1});
    console.log(Cookies.get('username'));
    console.log(holdUserName);
    console.log(holdPass);
    var logIn =
            {
                "username": holdUserName,
                "hash": holdPass
            };
    $.ajax({
        type: 'POST',
        async: false,
        cache: false,
        url: 'http://www.localhost:8080/library/login',
        contentType: "application/json",
        data: JSON.stringify(logIn),
        success: function (data, textStatus, xhr) {
            console.log(xhr.status);
            if (xhr.status == 200) {
                $(location).attr('href', 'runScript.html')
            }
        },
        error: function (error, xhr, ajaxOptions) {
            console.log(error);
            console.log(ajaxOptions);
            console.log(xhr.status);
            console.log(xhr.statusText);
        }
    })
});

Accept
/ Accept-Encoding gzip, deflate Accept-Language bg,en-US;q=0.7,en;q=0.3 Connection
keep-alive Content-Length
35 Content-Type
application/json; charset=UTF-8 Host
www.localhost:8080 Origin
null User-Agent
Mozilla/5.0 (Windows NT 6.1; WOW64; rv:43.0) Gecko/20100101 Firefox/43.0

rest service

@POST
@Path("login")
@Consumes({MediaType.APPLICATION_JSON})
public Response login(@HeaderParam("authorization") String auth, User usr){
    System.out.println(usr.getUsername());
    System.out.println(usr.getHash());
    Object ok = false;
    try {
        ok = (new LoginHandler(usr.getUsername(),usr.getHash()).call());
    } catch (Exception e) {
        e.printStackTrace();
    }
    if(ok.equals(true)){
        return Response.status(200).build();
    }else{
        return Response.status(403).build();
    }
}

When i try to make a request to the restful webservice with Postman, i get the response, and everything is ok, Chrome works too, but Firefox and IE are giving me a headache? Any help?

Much thanks!

UPDATE POSTED SCREEN OF ERROR

Screen of error

Paul Samsotha
  • 188,774
  • 31
  • 430
  • 651

0 Answers0