繁体   English   中英

jQuery AJAX调用服务器端方法不起作用

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM