首先普利文普拉薩德的所有答案,我覺得正確的。我只想添加一些關於「爲什麼...?」這個問題將會回答的描述。而不是「如何...?」。
如果你每HTTP GET發送到服務器的參數有一些特殊的字符,那就不會在URL中使用不編碼,所以你必須使用至少
url:'AjaxActions/OrderNotesUpdate.aspx?OrderID=' +
encodeURIComponent(OrderID) + '&Notes=' + encodeURIComponent($('#txtNotes').val())
下一步:您可以使用jQuery.param()
相對於的encodeURIComponent
和地點 '&' 字符paramters之間編碼URL參數:
$.ajax({
url:'AjaxActions/OrderNotesUpdate.aspx?' +
$.param({OrderID: OrderID, Notes: $('#txtNotes').val()}),
async:false})
或
$.ajax({
url:'AjaxActions/OrderNotesUpdate.aspx' +
data: { OrderID: OrderID, Notes: $('#txtNotes').val()},
async:false})
哪個地方'?' url
和data
之間編碼$.param
如果url
不包含'?'否則使用'&'代替。
下一步:只要有可能,您應該嘗試使用$.ajax
的異步版本。人們需要查看更多的代碼來幫助你。一般來說,它應該是
$.ajax({
url:'AjaxActions/OrderNotesUpdate.aspx' +
data: { OrderID: OrderID, Notes: $('#txtNotes').val()},
success:function(response) {
/* here use can use response.responseText. For examlpe you can
code which call the syncrone $.ajax before and used
the return value here */
}
})
有一個很好的理由使用'async:false',它會在請求期間鎖定客戶端瀏覽器。使用同步AJAX的最常見原因是缺乏對如何實現回調的理解。 – Matt 2010-09-25 10:15:18