java - Ajax 调用适用于 Google Chrome 但不适用于 IE 11

标签 java ajax rest google-chrome internet-explorer-11

我正在开发一个 RESTFul web 服务 项目,它有一个 POJO,如下所示:

@XmlRootElement
public class Input {
    //variable declarations 

   public Input(){
      //default constructor 
   }

   //constructor no 1
   public Input(String LR, double ECH,double CSH,String APP) {
        this.LR = LR;
        this.ECH = ECH;
        this.CSH = CSH;
        this.APP = APP;
    }

    //constructor no 2
    public Input(String LR, double ECH,double CSH,String APP,...) {
        this.LR = LR;
        this.ECH = ECH;
        this.CSH = CSH;
        this.APP = APP;
        //constructor of all other parameters including these
    }

//getters and setters method below.
}

我的 ajax 在这个按钮上被调用:

<button type="submit" onClick='functionname();' class="btn btn-primary" ><span class="glyphicon glyphicon-lock"></span>Function</button>

我的Controller类如下:

@Path("/input")
public class InputResponse {
InputService inputservice = new InputService();

@PUT
@Path("/approve")
@Produces(MediaType.APPLICATION_JSON)
public void approveInputRecord(Input obj) throws Exception{
    String LR = obj.getLR();
    double CSH = obj.getCSH();
    double ECH = obj.getECH();
    String APP = obj.getAPP();
    Input input = new Input(LR,CSH,ECH,APP);
    input = inputservice.approveTransaction(input);
    }
}

Service 类如下:

public class InputService {

CallableStatement stmt;
Statement commitStmt;

public InputService(){
    //database connection
}

public Input approveTransaction(Input input) throws SQLException {
    commitStmt = dcc.con.createStatement();
    stmt=dcc.con.prepareCall("BEGIN APPROVRTRANSACTION(?,?,?,?); END;");
    stmt.setString(1, input.getLR());
    stmt.setDouble(2, input.getECH());
    stmt.setDouble(3, input.getCSH());
    stmt.setString(4, input.getAPP());
    stmt.execute();
    commitStmt.executeQuery("COMMIT");
    return input;
}
}

在我的 JAVA Script 中,我对上面的 ajax 调用是:

    var obj = {
    LogReference : logreference,
    EuroclearHoldings:euroclearholdings,
    ClearstreamHoldings:clearstreamholdings,
    Approver : loginXPID
}
var jsonobj = JSON.stringify(obj);
$.ajax({
    url:'./webapi/input/approve',
    type: 'PUT',
    data:jsonobj,
    cache:false,
    contentType: 'application/json',
    dataType:'json',
    success:function(data)
    {
        alert('success');
    },
    error:function(xhr,textstatus,errorthrown){
        alert(xhr.responseText);
        alert(textstatus);
        alert(errorthrown);
    }
},'json');

将此作为我的代码,我的应用程序在 Google Chrome 上运行良好,但有时在 Internet Explorer 11 上运行有时无法运行。这是奇怪的行为。另一件我无法得到的事情是,即使它在 Chrome 上工作,ajax 调用总是让 alerts 出错。谁能解释为什么会这样?我该如何解决?非常感谢任何帮助。 更新

这是抛出错误时 chrome 上 network --> Response 选项卡上的输出。但尽管如此,我仍然得到了输出。 enter image description here

非常感谢

最佳答案

我可以看到你的Button type="submit" .如果它在form tag里面然后调用ajax requestaction的文件。正如我从上面的评论中看到的那样,这可能是问题所在。当您提交内容时,这会更改为 POST请求而不是 GET请求所以它给出了不允许的错误方法。查看解决方案只需更改 Button type='button'或调用 ajaxaction 上的 form tag .它应该工作。

关于java - Ajax 调用适用于 Google Chrome 但不适用于 IE 11,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33909654/

相关文章:

javascript - 保护 PHP 脚本 - Shopify

javascript - 防止以下系统中的手动 POST 请求

javascript - Ajax 添加选项以选择每个 json 数组

Java 流 : find an element with a min/max value of an attribute

java - 如何在 Java 中通过按钮输入值来创建序列?

忘记密码流程的 RESTFul API 设计建议

rest - 为什么代币被认为是无状态的?

rest - 在 Symfony 4 中验证 Rest Api

java - 在 WebSphere Application Server 中升级 JavaMail

java - 如何使用 H2、JPA 和 Hibernate 映射 JSON 列