使用jquery中的ajax進行post請求時,報錯,提示[object XMLHttpRequest]
阿新 • • 發佈:2019-01-27
問題描述:使用jquery進行ajax post請求時,報錯,提示[object XMLHttpRequest],在谷歌瀏覽器中發現此問題,IE7/8/9/10都沒問題
現象1:
現象2:
同時在事件檢視器中也相應發現如下事件內容:
Event code: 3005 Event message: 發生了未處理的異常。 Event time: 2013/7/3 15:53:23 Event time (UTC): 2013/7/3 7:53:23 Event ID: b3b2dd2f8f88403f87810651cfabbfb6 Event sequence: 74 Event occurrence: 1 Event detail code: 0 Application information: Application domain: /LM/W3SVC/2/ROOT-8-130173115807222308 Trust level: Full Application Virtual Path: / Application Path: D:\Project\Portal\code\MaintenanceSystem.Web\ Machine name: PC201305021047 Process information: Process ID: 3876 Process name: w3wp.exe Account name: PC201305021047\Administrator Exception information: Exception type: HttpException Exception message: 遠端主機關閉了連線。錯誤程式碼是 0x80070057。 Request information: Request URL: http://localhost:8001/WebService/DataSync.aspx Request path: /WebService/DataSync.aspx User host address: ::1 User: Is authenticated: False Authentication Type: Thread account name: PC201305021047\Administrator Thread information: Thread ID: 16 Thread account name: PC201305021047\Administrator Is impersonating: False Stack trace: 在 System.Web.Hosting.IIS7WorkerRequest.RaiseCommunicationError(Int32 result, Boolean throwOnDisconnect) 在 System.Web.Hosting.IIS7WorkerRequest.ExplicitFlush() 在 System.Web.HttpResponse.Flush(Boolean finalFlush) 在 System.Web.UI.HtmlTextWriter.Write(String s) 在 System.Web.UI.LiteralControl.Render(HtmlTextWriter output) 在 System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) 在 System.Web.UI.Page.Render(HtmlTextWriter writer) 在 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) Custom event details:
前臺請程式碼如下:
$.ajax({ url: '/WebService/DataSync.aspx', type: 'POST', data: { Action: "GETCATEGORYCODE", CategoryId: cid, ParentId: pid, Order: order }, dataType: 'text', timeout: 1000, error: function(ex) { alert(ex); }, success: function(result) { var r = result.split(":"); $('#<%=txtCategoryCode.ClientID %>').val(r[1]); if (r[0] == "true") { $('#<%=txtCategoryCode.ClientID %>').next().hide(); $('#<%=RequiredFieldValidator2.ClientID %>').show(); $('#<%=RequiredFieldValidator2.ClientID %>').html("編碼已存在"); $('#<%=btnSave.ClientID %>').attr("disabled", true); } else { $('#<%=txtCategoryCode.ClientID %>').next().show(); $('#<%=RequiredFieldValidator2.ClientID %>').hide(); $('#<%=btnSave.ClientID %>').attr("disabled", false); } } });
後臺DataSync.aspx.cs程式碼如下:
protected void Page_Load(object sender, EventArgs e)
{
Response.Clear();
Response.BufferOutput = false;
Response.ContentEncoding = System.Text.Encoding.UTF8;
switch (Action)
{
case DataSyncAction.GetCategoryCode:
GetCategoryCode();
break;
default:
break;
}
Response.Flush();
Response.End();
Response.Close();
}
解決方法:
發現在以上後臺程式碼中如果將Response.BufferOutput的值改為true現象1仍然存在,但現象2消失,如果進一步將以上程式碼中的Response.Flush();去掉的話則問題解決。
正確程式碼如下:
protected void Page_Load(object sender, EventArgs e)
{
Response.Clear();
Response.BufferOutput = true;
Response.ContentEncoding = System.Text.Encoding.UTF8;
switch (Action)
{
case DataSyncAction.GetCategoryCode:
GetCategoryCode();
break;
default:
break;
}
//Response.Flush();去掉此句程式碼
Response.End();
Response.Close();
}