我试图从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有什么问题? 谢谢!
答案 0 :(得分:2)
如果您在代码后面编写web方法,那么它应该是静态的。像这样改变你的网络方法
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();
}