简体   繁体   中英

posting JSON object with jquery

I have a JSON object:

var formData = {"field1":field1, "oper1":oper1, "value1":value1, "field2":field2, "oper2":oper2, "value2":value2, "field3":field3, "oper3":oper3, "value3":value3};

and I'm trying to post it with

$.post("<%=request.getRequestURL().toString()%>getXML.jsp", formData, function(response){getXML(response)});

I need the post to be formData = {json object} so I can get it from the request object with request.getParameter("formData").

However, everything is being posted by each key:value in the request, so I can't get the object by the formData name. I've tried stringifying the object and changing formData to {"formData":formData}in the jquery call, but nothing seems to be working. {"formData":formData} puts the parameters in this format:

formData[field1]    lname
formData[field2]    fname
formData[field3]    title
formData[oper1]     contain
formData[oper2]     contain
formData[oper3]     contain
formData[value1]    smith
formData[value2]    
formData[value3]

Is there a way to pass the object as the variable name "formData"?

You may use an relevant Model object that holds the json as a sting. After that you may post the model object.

使用以下方法解决了该问题:

$.post("<%=request.getRequestURL().toString()%>getXML.jsp", {"formData":JSON.stringify(formData)}, function(response){getXML(response)});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM