简体   繁体   English

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

[英]jQuery AJAX Call to Server Side Method Not Working

I am trying to call a server side method from a jQuery AJAX call but it is not working. 我试图从jQuery AJAX调用调用服务器端方法,但它无法正常工作。 Any help would be appreciated. 任何帮助,将不胜感激。

jQuery call is: jQuery调用是:

$('#btnAddAttachment').click(function () {

    $.ajax({
        type: "POST",
        url: "Ticket.aspx/AddAttachment",
        contentType: "application/json; charset=utf-8"
    }); 
});

Server Side code is: 服务器端代码是:

    [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();

    }

It appears that the call is getting back to the Ticket.aspx page but not getting to the AddAttachment method. 看来该调用正在返回Ticket.aspx页面,但没有进入AddAttachment方法。 Does anyone see anything wrong with the jQuery? 有没有人看到jQuery有什么问题? Thanks! 谢谢!

If you are writing a webmethod in code behind, it should be static. 如果你在后面编写一个webmethod,它应该是静态的。 Change your web method like this 像这样改变你的网络方法

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