我正在开发一个 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
选项卡上的输出。但尽管如此,我仍然得到了输出。
非常感谢
最佳答案
我可以看到你的Button type="submit"
.如果它在form tag
里面然后调用ajax request
在 action
的文件。正如我从上面的评论中看到的那样,这可能是问题所在。当您提交内容时,这会更改为 POST
请求而不是 GET
请求所以它给出了不允许的错误方法。查看解决方案只需更改 Button type='button'
或调用 ajax
在 action
上的 form tag
.它应该工作。
关于java - Ajax 调用适用于 Google Chrome 但不适用于 IE 11,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33909654/