这篇文章是JPA : Not able to persist record due to incorrect values being passed from client to server
的延续当我从服务器JSON输出中检索值时,格式如下。
{"emp":[{"dept":{"deptNo":"10","dname":"ACCOUNTING","loc":
"NEW YORK"},"empno":"7934","ename":"MILLER","hiredate":
"1982-01-23T00:00:00+03:00","job":"CLERK","mgr":"7782","sal":"1300"},
{"dept":
{"deptNo":"20","dname":"RESEARCH","loc":"DALLAS"},"empno":"7902","ename":
"FORD",
"hiredate":"1981-12-03T00:00:00+03:00","job":"ANALYST","mgr":"7566","sal":
"3000"},{"dept":
{"deptNo":"30","dname":"SALES","loc":"CHICAGO"},"empno":"7900","ename":
"JAMES", "hiredate":
"1981-12-03T00:00:00+03:00","job":"CLERK","mgr":"7698","sal":"950"},
{"dept":{"deptNo":"20","dname":"RESEARCH","loc":"DALLAS"},"empno":"7876",
"ename":
"ADAMS","hiredate":
"1987-05-23T00:00:00+03:00","job":"CLERK","mgr":"7788","sal":"1100"},
{"comm":"0","dept":
{"deptNo":"30","dname":"SALES","loc":"CHICAGO"},"empno":"7844","ename":
"TURNER","hiredate":
"1981-09-08T00:00:00+03:00","job":"SALESMAN","mgr":"7698","sal":"1500"},
{"dept":{"deptNo":"10","dname":"ACCOUNTING","loc":
"NEW YORK"},"empno":"7839","ename":"KING","hiredate":
"1981-11-17T00:00:00+03:00","job":"PRESIDENT","sal":"5000"},
{"dept":"deptNo":"20","dname":"RESEARCH","loc":"DALLAS"},"empno":"7788",
"ename":"SCOTT","hiredate":
"1987-04-19T00:00:00+03:00","job":"ANALYST","mgr":"7566","sal":"3000"},
{"dept":{"deptNo":"10","dname":"ACCOUNTING","loc":
"NEW YORK"},"empno":"7782","ename":"CLARK","hiredate":
"1981-06-09T00:00:00+03:00","job":"MANAGER","mgr":"7839","sal":"2450"},
{"dept":"deptNo":"30","dname":"SALES","loc":"CHICAGO"},"empno":"7698",
"ename":
"BLAKE","hiredate":
"1981-05-01T00:00:00+03:00","job":"MANAGER","mgr":"7839","sal":"2850"},
{"comm":"1400","dept":
{"deptNo":"30","dname":"SALES","loc":"CHICAGO"},"empno":"7654","ename":
"MARTIN","hiredate":
"1981-09-28T00:00:00+03:00","job":"SALESMAN","mgr":"7698","sal":"1250"},
{"dept":
{"deptNo":"20","dname":"RESEARCH","loc":"DALLAS"},"empno":"7566","ename":
"JONES","hiredate":
"1981-04-02T00:00:00+03:00","job":"MANAGER","mgr":"7839","sal":"2975"},
{"comm":"500","dept":
{"deptNo":"30","dname":"SALES","loc":"CHICAGO"},"empno":"7521","ename":
"WARD", "hiredate":
"1981-02-22T00:00:00+03:00","job":"SALESMAN","mgr":"7698","sal":"1250"},
{"comm":"300","dept":
{"deptNo":"30","dname":"SALES","loc":"CHICAGO"},"empno":"7499",
"ename":
"ALLEN","hiredate":"1981-02-20T00:00:00+03:00","job":"SALESMAN","mgr":
"7698",
"sal":"1600"},{"dept":
{"deptNo":"20","dname":"RESEARCH","loc":"DALLAS"},"empno":"7369","ename":
"SMITH","hiredate":
"1980-12-17T00:00:00+03:00","job":"CLERK","mgr":"7902","sal":"800"}]}
如果我想将数据插入到EMP表中,如何预期数据以及如何将数据从客户端JSON传递到服务器?
我尝试了以下格式,但它没有帮助。
function formToJSON() {
return JSON.stringify({
"deptNo": "10",
"empno": "9004",
"ename": "JILL",
"job": "mfg",
"mgr": 34,
"sal": 456,
"comm": 900
});
}
$(document).ready(function() {
$("#btnSubmit").click(function() {
$.ajax({
url: "Test1/rest/employee/",
type: "POST",
data: formToJSON(),
contentType: "application/json; charset=utf-8",
dataType: "json",
// console : 'ok',
})
});
});
当我从浏览器提交时,当我在服务器中收到时,deptNo为零。 它在服务器中的预期效果如何?
答案 0 :(得分:1)
实际上你向服务器发送了错误的json格式,这就是你得到deptNo = 0的原因。
正确的Json格式是这样的。
{
"deptNo":{
"deptNo":500,
"dname":"RESEARCH",
"loc":"DALLAS"
},
"empno":7876,
"ename":"ADAMS",
"job":"1100",
"sal":"7788"
}
您可以将json字符串化为
function formToJSON() {
var test = { empno:7876, ename:'ADAMS', job:1100, sal:7788,
deptNo:{deptNo:6, dname:'RESEARCH', loc:'DALLAS'}
};
return JSON.stringify(test);
}
JPA的续篇:Not able to persist record due to incorrect values being passed from client to server
还有这种json格式问题。