我的应用程序在前端动态生成了id,这需要映射回后端的模型。为此,我将一个索引传递给ajax调用,然后将其用于后端的映射目的,但javascript将此标记为ajax调用中data属性的语法错误。
function getStuff (stuffIndex,stuffType){
event.preventDefault();
var stuffName = $('#stuffName'+ index).val();
$.ajax({
url : flowExecutionUrl,
data : {
'stuff['+stuffIndex+'].name': stuffName,
ajaxSource:"StuffTypes_" + stuffIndex
},
success : function(html) {
}
});
}
此行发生语法错误:'stuff ['+'stuffIndex'+']。name':stuffName,
答案 0 :(得分:1)
function getStuff (stuffIndex,stuffType){
event.preventDefault();
var stuffName = $('#stuffName'+ index).val(),
data = {},
dynamicData = 'stuff['+stuffIndex+'].name';
data[dynamicData] = stuffName;
data['ajaxSource'] = 'StuffTypes_' + stuffIndex;
$.ajax({
url : flowExecutionUrl,
data : data,
success : function(html) {
alert('success');
},
error: function(){
alert('failure');
}
});
}