我有以下web服務:如何將JavaScript數組的數組傳遞給Web服務?
[webmethod]
public string MakeReservation(?? PassengersInfo)//what data type use for PassengerInfo
{
}
,並在JavaScript中我有下面的代碼。
var ResultInfo = new Array();
$("#GrdPassengerInformationMakeReservation").find('tr:not(:first)').each(function() {
var Info = new Array();
Info.push($(this).find('td:eq(1)').text());
Info.push(($(this).find('td:eq(2)')).find('select').val());
Info.push(($(this).find('td:eq(3)')).find('input').val());
Info.push(($(this).find('td:eq(4)')).find('input').val());
Info.push(($(this).find('td:eq(5)')).find('select').val());
Info.push(($(this).find('td:eq(6)')).find('input').val());
ResultInfo.push(Info);
})
$.ajax(
{ url: "Ajaxes/Reservation.asmx/MakeReservation",
contentType: "application/json; charset=utf-8",
dataType: "json",
type: "POST",
data: "{'PassengersInfo':'" + ResultInfo + "'}",
async: false,
success: function(data) {
}
});
我想通過ResultInfo
到webservice。 我應該在我的web服務中使用什麼數據類型?
請...... **絕不** **用字符串函數手動生成JSON!只需包含https://github.com/douglascrockford/JSON-js/blob/master/json2.js並使用'JSON.stringify()'! – ThiefMaster 2012-03-29 06:08:27
爲什麼不能?它有什麼好處? – 2012-03-29 06:46:51
與使用字符串函數不構建XML的原因相同:您不需要關心轉義事件,括號嵌套等。除此之外,您不能簡單地將字符串與數組連接,因此您的代碼不會'data:JSON.stringify({PassengersInfo:ResultInfo})'工作時會像魅力一樣工作。 – ThiefMaster 2012-03-29 06:49:36