我正在尝试将json对象从js文件发送到servlet,并在servlet中尝试获取价值,但获取Null如何解析plz建议无法从json对象获取数据
JSON对象:
var Record ={
Username:document.getElementById("username").value,
Name:document.getElementById("name").value,
Contact:document.getElementById("phone").value,
Email:document.getElementById("Email").value,
DOB:document.getElementById("date").value,
Team:team,
Position:Position,
PermanentAddress:document.getElementById("per_add").value,
PermanentCountry:document.getElementById("country").value,
permanentState:document.getElementById("state").value,
permanentCity:document.getElementById("city").value,
CurrentAddress:document.getElementById("CurrentAddress").value,
CurrentCountry:document.getElementById("currentCountry").value,
CurrentState:document.getElementById("currentState").value,
CurrentCity:document.getElementById("currentCity").value
}
发送数据:
var PlayerData = JSON.stringify(Record);
$.post('/FootballLeagueController', PlayerData)
Inservlet:
Gson gson=new Gson();
FootballPlayerData playerData=new Gson().fromJson(request.getParameter("PlayerData")
,FootballPlayerData.class); //(comment)Note :-FootballPlayerData is my pojo class.
答案 0 :(得分:0)
request.getParameter(String)将为您提供post参数“ PlayerData”的值,在这种情况下不存在,因为您只是将JSON String放入了post主体中。 要具有名为“ PlayerData”的参数,可以执行以下操作:
([1-9]\\s)*[1-9]$
这将发送一个名为“ PlayerData”的参数,并将字符串化的JSON作为值。
请阅读并了解jquery.post()文档以了解更多信息