断点没有打到ajax调用

时间:2016-08-01 08:21:51

标签: jquery .net ajax visual-studio-2012

我正在尝试对.aspx WebMethod进行GET ajax调用,但没有让断点符合方法。奇怪的是,我在浏览器控制台和Visual Studio级别都没有收到任何错误。这是ajax语法。

var appName = "FSDB";

$.ajax({
    type: "GET",
    url: "Default.aspx//ConvertDatadttoString",
    data: appName,
    success: function (data) {
        alert("Hi");
        console.log(data);
    }
});

我收到成功的"Hi"消息。请帮忙。

更新 方法签名

    [WebMethod]
    public static string ConvertDatadttoString(string appName)
    {

        DataTable dt = new DataTable();

        dt.Columns.Add("ID", typeof(int)); // Add five columns.
        dt.Columns.Add("Name", typeof(string));
        dt.Columns.Add("EmailId", typeof(string));

        dt.Rows.Add(15, "Vikas", "gh.com");
        dt.Rows.Add(40, "Pankaj", "pa.com");

        System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
        List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
        Dictionary<string, object> row;
        foreach (DataRow dr in dt.Rows)
        {
            row = new Dictionary<string, object>();
            foreach (DataColumn col in dt.Columns)
            {
                row.Add(col.ColumnName, dr[col]);
            }
            rows.Add(row);
        }
        return serializer.Serialize(rows);
    }
}

1 个答案:

答案 0 :(得分:0)

我想说默认情况下,页面的webmethods不允许GET请求。尝试使用ScriptService属性修饰webmethod,并将UseHttpGet参数值设置为TRUE,如下所示:

[WebMethod]
[ScriptMethod(UseHttpGet=true)]
public static string ConvertDatadttoString(string appName)

ajax调用应该如下:

           $.ajax({
                type: "GET",
                url: "Default.aspx//ConvertDatadttoString?appName='" + appName + "'",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (data) {
                    alert("Hi");
                    console.log(data);
                }
            });