[英]jQuery AJAX Call to Server Side Method Not Working
我試圖從jQuery AJAX調用調用服務器端方法,但它無法正常工作。 任何幫助,將不勝感激。
jQuery調用是:
$('#btnAddAttachment').click(function () {
$.ajax({
type: "POST",
url: "Ticket.aspx/AddAttachment",
contentType: "application/json; charset=utf-8"
});
});
服務器端代碼是:
[WebMethod]
public void AddAttachment()
{
string name = txtAttach.FileName;
string strPath = ConfigurationManager.AppSettings["crmWorkspacesDir"].ToString() + txtTicketNum.Text + "\\";
if (!Directory.Exists(strPath))
Directory.CreateDirectory(strPath);
txtAttach.SaveAs(strPath + name);
DataTable oDT = (DataTable)ViewState["attachments"];
DataRow oDR = oDT.NewRow();
oDR["File"] = strPath + name;
oDR["Size"] = new FileInfo(strPath + name).Length / 1000;
oDT.Rows.Add(oDR);
grdAttachments.DataSource = oDT;
grdAttachments.DataBind();
}
看來該調用正在返回Ticket.aspx頁面,但沒有進入AddAttachment方法。 有沒有人看到jQuery有什么問題? 謝謝!
如果你在后面編寫一個webmethod,它應該是靜態的。 像這樣改變你的網絡方法
public static void AddAttachment()
{
string name = txtAttach.FileName;
string strPath = ConfigurationManager.AppSettings["crmWorkspacesDir"].ToString() + txtTicketNum.Text + "\\";
if (!Directory.Exists(strPath))
Directory.CreateDirectory(strPath);
txtAttach.SaveAs(strPath + name);
DataTable oDT = (DataTable)ViewState["attachments"];
DataRow oDR = oDT.NewRow();
oDR["File"] = strPath + name;
oDR["Size"] = new FileInfo(strPath + name).Length / 1000;
oDT.Rows.Add(oDR);
grdAttachments.DataSource = oDT;
grdAttachments.DataBind();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.