我试图通过ajax调用将一些变量通过JSON从JSP传递给servlet。但我在servlet端获得null值。请有人帮助我找出我犯错的地方/我错过的地方
package com.example;
import org.kie.api.KieServices;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
public class SalaryTest {
public static final void main(String[] args) {
try {
// load up the knowledge base
KieServices ks = KieServices.Factory.get();
KieContainer kContainer = ks.getKieClasspathContainer();
KieSession kSession = kContainer.newKieSession("ksession-dtables");
Salary a = new Salary();
a.setAmount(600);
kSession.insert(a);
kSession.fireAllRules();
} catch (Throwable t) {
t.printStackTrace();
}
}
public static class Salary{
private String message;
private int amount;
private int status;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public int getAmount() {
return amount;
}
public void setAmount(int amount) {
this.amount = amount;
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
}
}

//JSON
var masterdata = new Object();
masterdata.grn = $('#grn').val();
masterdata.pono = $('#pono').val();
masterdata.podt = $('#podt').val();
//call the servlet to insert the data only when error = 0
if (error != 1){
$.ajax({
url : 'insertserv',
type: 'POST',
dataType: 'json',
data: {test : JSON.stringify(masterdata)},
contentType: 'application/json',
mimeType: 'application/json',
success : function(data) {
alert('Hi');
}
});
}
else{
alert("Save cannot be performed. Please check the entered data!");
}
});

答案 0 :(得分:2)
用我的代码替换你的ajax代码......
//JSON
var masterdata = new Object();
masterdata.grn = $('#grn').val();
masterdata.pono = $('#pono').val();
masterdata.podt = $('#podt').val();
//call the servlet to insert the data only when error = 0
if (error != 1){
$.ajax({
url : 'insertserv',
type: 'POST',
dataType: 'json',
data: JSON.stringify({"test" :masterdata}),
contentType: 'application/json',
mimeType: 'application/json',
success : function(data) {
alert('Hi');
}
});
}
else{
alert("Save cannot be performed. Please check the entered data!");
}
});
在servlet中获取数据
BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream()));
String json = "";
if (br != null) {
json = br.readLine();
}
JSONObject wholedata= new JSONObject(json);
现在对象wholedata有你的json ..
如果你正在使用JSON.stringify(),那么你必须使用
servlet
中的BufferedReader,
当您在servlet
的网址中传递数据时,可以在servlet
中使用request.getparameter。
答案 1 :(得分:0)
如果您的后端使用json
内容进行回复,则只有dataType:"json"
有效。尝试更改响应类型:
response.setContentType("application/json");
因为你的ajax期望来自后端的json dataType:"json",
。
反之亦然。
将dataType更改为text dataType:"text",
,因为响应标头显示为response.setContentType("text/plain");
。但在这种情况下,您必须使用JSON.parse()
来解析json字符串。
//JSON
var masterdata = new Object();
masterdata.grn = $('#grn').val();
masterdata.pono = $('#pono').val();
masterdata.podt = $('#podt').val();
//call the servlet to insert the data only when error = 0
if (error != 1) {
$.ajax({
url: 'insertserv',
type: 'POST',
dataType: 'text', //<------change this
data: {
test: JSON.stringify(masterdata)
},
contentType: 'application/json',
mimeType: 'application/json',
success: function(data) {
alert('Hi');
}
});
} else {
alert("Save cannot be performed. Please check the entered data!");
}
});