【问题标题】:Ajax call working on Google Chrome but not on IE 11Ajax 调用在 Google Chrome 上工作,但在 IE 11 上不工作
【发布时间】:2015-12-09 19:22:42
【问题描述】:

我正在开发一个RESTFul web service 项目,它有一个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 --&gt; Response 选项卡上的输出。但尽管如此,我仍然得到输出。

非常感谢

【问题讨论】:

  • 您可以尝试使用POST请求而不是PUT
  • @MadushanPerera 如果我使用POSTPUT 它会给出同样的问题。但是对于GET,它不能在任何一个浏览器上运行并给出错误no viable alternative at input '&lt;EOF&gt;'
  • 能否请您从 Chrome 浏览器的“网络”选项卡中提供服务器响应。
  • @MeetJoeBlack 请检查我的编辑并告诉我你的想法。
  • 对不起,但您可能只提供一个带有“批准 xhr”呼叫表单“网络”选项卡的屏幕(现在是红色),因此您必须点击它并制作一个“响应”屏幕标签。请更换你的大屏幕。

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


【解决方案1】:

我可以看到你的Button type="submit"。如果它在form tag 内,则在文件的action 中调用ajax request。正如我从上面的 cmets 看到的,这可能是问题所在。当您提交某些内容时,这将更改为 POST 请求而不是 GET 请求,因此不允许给出错误方法。查看解决方案只需更改Button type='button' 或在form tagaction 上调用ajax。它应该工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-24
    • 2017-07-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多