我正在创建一个依赖 JSON 将表单提交到数据库的移动应用程序。我有我的提交功能:
function sendJson(service, method, json) {
var request = $.ajax ({
type: 'POST',
url: '/remoteserver/service/' + service + '/' + method,
dataType: 'json',
async: false,
data: JSON.stringify(json),
success: function (msg) {
alert('Success ' + JSON.stringify(msg.location));
},
error: function(msg) {
alert('YOU SUCK' + JSON.stringify(msg));
}
});
}
目前正在使用这样的东西来填充 JSON 字符串:
$("element").click(function(){
var wrapper = {};
var location = {};
wrapper.location = location;
location.name = $('#name').val();
location.address1 = $('#address1').val();
location.address2 = $('#address2').val();
location.city = $('#city').val();
location.state = $('#state').val();
location.country = $('#country').val();
location.zipCode = $('#zipCode').val();
location.contactName = $('#contactName').val();
location.contactPhone = $('#contactPhone').val();
sendJson("locationService", "createLocation", wrapper);
});
我的问题是——我将在这个应用程序中拥有近 100 个表格。如何让每个 JSON 元素(? - IE location.name)映射到表单字段而无需明确说明location.name = $('#name).val();
,或者这是在 JSON 中如何完成的?我进行了广泛的搜索,一切似乎都不适合我想做的事情。谢谢你。