ajax post call在Firefox和IE中不起作用,Chrome可以正常工作

时间:2016-01-04 19:14:53

标签: javascript java ajax rest jersey

所以我有这个问题,我有一个Java后端,它基本上是宁静的服务+数据库层,它从Javascript前端回答查询。

我有一个javascript ajax请求,可以在Chrome中完美运行,但在使用Firefox / IE时甚至无法访问服务器。

AJAX错误是:  readyState = 0,status = 0,statusText =“error”

这是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调用

$(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);
        }
    })
});
  

接受
   /   接受编码   gzip,deflate   接受语言   BG,EN-US; Q = 0.7,连接; Q = 0.3   连接
  活着   内容长度
  35   内容类型
  应用/ JSON;字符集= UTF-8   主机
  www.localhost:8080   产地
  空值   用户代理
  Mozilla / 5.0(Windows NT 6.1; WOW64; rv:43.0)Gecko / 20100101 Firefox / 43.0

休息服务

@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();
    }
}

当我尝试向Postman提出对其余网络服务的请求时,我得到了回复,一切正常,Chrome也有效,但Firefox和IE让我头疼?有什么帮助吗?

非常感谢!

更新 发布错误屏幕

Screen of error

0 个答案:

没有答案