我怀疑如何将 json 数据拆分为字符串。我的意图是动态创建几个文本框并使用 ajax 发布它们,以避免完全回发,这里检查我的 ajax 部分
function profileAndSectin_Submit() {
//alert('test123');
document.getElementById("hiddenSection").value = i;
$('#profile_form').submit(
function() {
alert(i);
var profileName = $('#profileName').val();
var Section1 = $('#Section1').val();
var dynamicData = " ";
for ( var m = 2; m <= i; m++) {
var textbx = $('#Section' + m).val();//"Section"+m;
var dt="section"+m+":"+textbx;
//var txtbxval = document.getElementsByName(textbx).value;
var x = textbx ;
if (m <= m - 1) {
x + " ";
}
dynamicData = dynamicData +dt+",";
//dynamicData = dynamicData +":"+dt+",";
}
alert(dynamicData);
var Data = "profileName :"+profileName+","+"Section1 :"+Section1+"," + dynamicData;
alert(Data);
$.ajax({
type : "post",
url : "addProfile",
//data : {
//"profileName" : profileName,
//"Section1" : Section1,
//},
data :{"Count" : i,"Data" :Data},
success : function(msg) {
alert(i);
$('#divContent').load('addfields.jsp');
},
Error : function(msg) {
debugger;
}
});
return false;
});
};
我将这些值发送到一个控制器,主要问题出现在这里,我如何检索这些数据,如果我使用@RequestParam("Data")
字符串数据,那么我将总数据作为单个字符串获取,比如
{ProfileName:profileName,Section1:section1,Section2:section2...}
但我希望每个字符串都像我想要的 profileName 、Section2 和 Section3 一样。
控制器.java
@Controller
public class SettingController implements HibernateConfig {
@RequestMapping(value = "/addProfile", method = RequestMethod.POST)
//public String home(HttpServletRequest request,@RequestParam("Count") int i,@RequestParam("profileName") String pname,@RequestParam("Section1") String Section1,Locale locale, Model model) throws IOException
public String home(HttpServletRequest request,@RequestParam("Count") int i,@RequestParam("Data") String data,Locale locale, Model model) throws IOException
{
System.out.println(i);
System.out.println(data);
}
所以有什么想法吗?