如何在完全更新对象后通过ajax发送对象?
我有这个js事件中代码:如何在完全更新对象后通过ajax发送对象?
var userData = tableWidget.grid('userData');
console.log(tableWidget.grid('userData'));
$.ajax({
"url": "../../server/query.aspx?tableEvent=reordercolumns&table=SubContractor",
"data": { dataColumnOrder: JSON.stringify(userData) },
"dataType": "json",
"type": "GET",
"error": function() {
console.log("Something wrong with the columns' reorder saving process");
}
});
比方说,用户数据有两个条件。之前和在事件被触发后:
userData before event
:[a, b, c]
userData after event
:[a, c, b]
在我的情况下,当事件被激活时,console.log(tableWidget.grid('userData'));
包含userData after event
理所应当的。但是,当我通过ajax发送它时,在服务器中,对象包含userData before event
。我假设ajax在对象完全更新之前发送对象。
我的问题是,在var userData = tableWidget.grid('userData');
被完全更新后,有什么办法可以执行ajax
?
更新:事实证明,问题似乎发生在stringify
函数。这是我与客体的截图没有stringify
:
没有字符串化console.log(userData);
:
随着字符串化console.log(JSON.stringify(userData));
:
{
"view": "SubContractorGridView",
"title": "SubContractor Grid View",
"settings": {
"colOrder": ["Id", "ExtKey", "InsertionDate", "DeletionDate", "Name", "Address", "ZipCode", "EmailAddress", "PhoneNumber", "UserName", "Password", "LastUpdate", "Price"],
"orderBy": "Name",
"orderDir": "asc"
},
"columns": [{
"dbField": "Id",
"show": true,
"widthMobile": 90,
"widthPhablet": 89,
"widthMiniTablet": 90,
"widthTablet": 90,
"widthSmallDesktop": 90,
"widthDesktop": 90,
"widthLargeDesktop": 90
}, {
"dbField": "ExtKey",
"show": false,
"widthMobile": 90,
"widthPhablet": 120,
"widthMiniTablet": 90,
"widthTablet": 90,
"widthSmallDesktop": 150,
"widthDesktop": 90,
"widthLargeDesktop": 100
}, {
"dbField": "InsertionDate",
"show": true,
"widthMobile": 90,
"widthPhablet": 100,
"widthMiniTablet": 90,
"widthTablet": 90,
"widthSmallDesktop": 100,
"widthDesktop": 90,
"widthLargeDesktop": 110
}, {
"dbField": "DeletionDate",
"show": false,
"widthMobile": 90,
"widthPhablet": 90,
"widthMiniTablet": 90,
"widthTablet": 90,
"widthSmallDesktop": 90,
"widthDesktop": 90,
"widthLargeDesktop": 100
}, {
"dbField": "Name",
"show": true,
"widthMobile": 90,
"widthPhablet": 130,
"widthMiniTablet": 90,
"widthTablet": 90,
"widthSmallDesktop": 90,
"widthDesktop": 90,
"widthLargeDesktop": 90
}, {
"dbField": "Address",
"show": true,
"widthMobile": 90,
"widthPhablet": 90,
"widthMiniTablet": 90,
"widthTablet": 90,
"widthSmallDesktop": 90,
"widthDesktop": 90,
"widthLargeDesktop": 90
}, {
"dbField": "ZipCode",
"show": true,
"widthMobile": 90,
"widthPhablet": 90,
"widthMiniTablet": 90,
"widthTablet": 90,
"widthSmallDesktop": 90,
"widthDesktop": 90,
"widthLargeDesktop": 90
}, {
"dbField": "EmailAddress",
"show": true,
"widthMobile": 90,
"widthPhablet": 90,
"widthMiniTablet": 90,
"widthTablet": 90,
"widthSmallDesktop": 90,
"widthDesktop": 90,
"widthLargeDesktop": 90
}, {
"dbField": "PhoneNumber",
"show": true,
"widthMobile": 90,
"widthPhablet": 90,
"widthMiniTablet": 90,
"widthTablet": 90,
"widthSmallDesktop": 90,
"widthDesktop": 90,
"widthLargeDesktop": 90
}, {
"dbField": "UserName",
"show": true,
"widthMobile": 90,
"widthPhablet": 90,
"widthMiniTablet": 90,
"widthTablet": 90,
"widthSmallDesktop": 90,
"widthDesktop": 90,
"widthLargeDesktop": 90
}, {
"dbField": "Password",
"show": true,
"widthMobile": 90,
"widthPhablet": 90,
"widthMiniTablet": 90,
"widthTablet": 90,
"widthSmallDesktop": 90,
"widthDesktop": 90,
"widthLargeDesktop": 90
}, {
"dbField": "LastUpdate",
"show": true,
"widthMobile": 90,
"widthPhablet": 90,
"widthMiniTablet": 90,
"widthTablet": 90,
"widthSmallDesktop": 90,
"widthDesktop": 90,
"widthLargeDesktop": 90
}, {
"dbField": "Price",
"show": true,
"widthMobile": 90,
"widthPhablet": 90,
"widthMiniTablet": 90,
"widthTablet": 90,
"widthSmallDesktop": 90,
"widthDesktop": 90,
"widthLargeDesktop": 90
}
]
}
正如你可以看到列的顺序是有点不用stringify改变。
我认为你必须尝试这样......
var userData = tableWidget.grid('userData');
console.log(tableWidget.grid('userData'));
$.ajax({
url: "../../server/query.aspx?tableEvent=reordercolumns&table=SubContractor",
data: { 'dataColumnOrder': userData},
dataType: "json",
type: "POST",
success:function(response){
//write JS when ajax call success
console.log('success');
},
error: function() {
console.log("Something wrong with the columns' reorder saving process");
}
});
不幸的是,我也不工作。 –
你可以发布你的错误? –
也没有错误。对于ajax来说,它工作正常,只有对象,它不会在服务器中更新,即使对象已经在前端进行了更新。 –
@RoryMcCrossan是啊,没错。如果ajax在更新之前发送对象,是否有可能?我的意思是,它首先在var userData = tableWidget.grid('userData');'之前执行ajax。 –