Jquery AJAX没有触发一些c#webmethods

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (42)

我遇到了一个奇怪的问题。我正在从我的页面处理几个AJAX调用。有些工作和其他人不会触发相应的方法,而是返回页面的完整HTML。

这是有效的代码:JS:

/***************************************************************
function getBoothData():

retrieve data for site on site change or page load
/**************************************************************/
function getBoothData() {
    var location = $("input[name=siteMap]:checked").val();
    $(".desk").remove();
    //Get booth data
    $.ajax({
        type: "POST",
        url: "./default.aspx/deskData",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        data: JSON.stringify({ "location": location }),
        success: function (response) {
            siteID = location;
            data = response.d;
            showData(data.Desks);
        },
        failure: function (response) {
            alert(response.d);
        }
    }); //$.ajax()
} //getBothData()
/**************************************************************
end getBoothData()
/**************************************************************/

C#:

[WebMethod]
    public static DeskItemList deskData(string location)
    {
        DataTable dt = new DataTable();

        string query = "select * from tblSiteLayout where SiteID=(select ID from tblSiteLayoutSites where UPPER(SiteName)=UPPER('{0}'))";
        query = string.Format(query, location);
        SqlConnection con;
        SqlCommand cmd = getCmd(query, out con);
        con.Open();
        dt.Load(cmd.ExecuteReader());
        con.Close();

        var DeskList = new DeskItemList();
        DeskList.Desks = new List<desk>();
        foreach (DataRow dr in dt.Rows)
        {
            desk d = new desk();
            d.id = dr["id"].ToString();
            d.x = Convert.ToInt32(dr["x"]);
            d.y = Convert.ToInt32(dr["y"]);
            d.height = Convert.ToInt32(dr["height"]);
            d.width = Convert.ToInt32(dr["Width"]);
            d.UserLogon = dr["UserLogon"].ToString().Trim();
            d.DeskID = dr["DeskID"].ToString().Trim();
            d.Extension = dr["Extension"].ToString().Trim();
            d.Assets = dr["Assets"].ToString().Trim();
            d.computerIP = dr["ComputerIP"].ToString().Trim();
            d.RAM = dr["RAM"].ToString().Trim();
            d.HDD = dr["HDD"].ToString().Trim();
            d.SiteID = dr["SiteID"].ToString().Trim();
            d.Department = dr["Department"].ToString().Trim();
            d.deskType = (int)dr["deskType"];
            d.rotation = (int)dr["rotation"];
            d.UserName = dr["UserName"].ToString();

            // d.usrImg = dr["userImg"].ToString();

            DeskList.Desks.Add(d);
        }

        return DeskList;
    }

上面的调用完全有效,而以下函数根本没有被调用:

JS:

/***************************************************************
function fillData(obj):

Takes associated data from element and applies it to 
the edit form
/**************************************************************/
function fillData(obj) {
    //add data to element to show and edit from editData div
    $("#rowID").text(obj.data("rowID"));
    $("#deskID").val(obj.data("deskID"));
    $("#user").text(obj.data("user"));
    $("#empID").text(obj.data("empID"));
    $("#ext").text(obj.data("ext"));
    $("#dept").text(obj.data("dept"));

    var desk=obj.data("rowID");

    //retrieve user image
    $.ajax({
        type: "post",
        url: "default.aspx/getImageForDesk",
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        data: '{ "deskID":' + desk + '}',
        success: function (response) {
            var data = response.d;
            $("#userImg").attr('src',data);
        },
        failure: function (response) {
            alert(response.d);
        },
         error: function (resp, textStatus, errorThrown) {
            console.log(errorThrown);
            //alert("Status: " + textStatus); 
            //alert("Error: " + XMLHttpRequest.responseText);
            var a=resp.responseText;

        },
        complete: function (resp, status){
            //console.log(resp.responseText);
            //alert(status);
        }
    }); //$.ajax()
}
/**************************************************************
end fillData()
/**************************************************************/

C#:

[WebMethod]
    public string getImageForDesk(int deskID)
    {
        string ret = "";
        //if (Convert.ToInt32(deskId) > 1)
        int deskId = deskID;

        if ((int)deskId >= 1)
        {

            string query = "select usrImage from tblSiteLayout where id=" + deskId;
            SqlConnection con = MCAIntranet.Data.Connections.ConDEV();
            SqlCommand cmd = new SqlCommand(query, con);
            con.Open();
            byte[] img = (byte[])cmd.ExecuteScalar();
            con.Close();

            System.IO.File.WriteAllBytes(Server.MapPath("./images/tempUser.png"), img);

            ret = "tempUser.png";
        }
        return ret;
        //        return JsonConvert.SerializeObject(new {image = ret});
    }

无论出于何种原因,即使(据我所知),呼叫和接收签名都是相同的。有错误函数只是告诉我它不能将返回的HTML解析为JSON,所以对我来说没用。关于我在哪里出错了,有什么建议吗?

谢谢

提问于
用户回答回答于

Web方法必须是静态的。我没有使用静态的原因是因为我想保存文件以便使用图像。通过返回Base64字符串并将其用作图像源来解决此问题。

扫码关注云+社区

领取腾讯云代金券