JSONオブジェクトをJavaScriptからjavaサーバーレットに送信します。 Javaコード次JavaサーバーレットJSONデータを解析します
var a = {};
function SaveInfo(c)
{
document.getElementById("saveButton").className = "savebutton";
document.getElementById("cancelButton").className = "cancebutton";
var z= $(c).parent().parent().attr('id');
a[z] = c.value;
}
$("#saveButton").click(function(e){
if( document.getElementById("saveButton").className=="savebutton")
$.ajax({
url: "../../update",
type: 'POST',
dataType: 'json',
data: JSON.stringify(a),
contentType: 'application/json',
mimeType: 'application/json',
success: function (data) {
consol.log(data);
},
error:function(data,status,er) {
alert("error: "+data+" status: "+status+" er:"+er);
}
});
});
と::
public void doPost(HttpServletRequest req, HttpServletResponse res){
JSONObject jObj;
jObj = new JSONObject(req.getParameter("mydata")); // this parses the json
Iterator it = jObj.keys();
while(it.hasNext())
{
String key = (String)it.next(); // get key
Object o = jObj.get(key); // get value
// store in session
}
}
JSONが正常に受信され は、私は次のJavaScriptコードを使用していました。私はjavascriptを経由して渡された両方の値とキーを読みたい
jObj = new JSONObject(req.getParameter("mydata"));
: 問題には、コンストラクタはありませんです。
ありがとうございました! – Shomit