【发布时间】: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 --> Response 选项卡上的输出。但尽管如此,我仍然得到输出。
非常感谢
【问题讨论】:
-
您可以尝试使用
POST请求而不是PUT。 -
@MadushanPerera 如果我使用
POST或PUT它会给出同样的问题。但是对于GET,它不能在任何一个浏览器上运行并给出错误no viable alternative at input '<EOF>' -
能否请您从 Chrome 浏览器的“网络”选项卡中提供服务器响应。
-
@MeetJoeBlack 请检查我的编辑并告诉我你的想法。
-
对不起,但您可能只提供一个带有“批准 xhr”呼叫表单“网络”选项卡的屏幕(现在是红色),因此您必须点击它并制作一个“响应”屏幕标签。请更换你的大屏幕。
标签: java ajax rest google-chrome internet-explorer-11