我有這個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改變。
@RoryMcCrossan是啊,沒錯。如果ajax在更新之前發送對象,是否有可能?我的意思是,它首先在var userData = tableWidget.grid('userData');'之前執行ajax。 –