Ajax调用webservice

时间:2015-02-07 23:40:15

标签: c# ajax xml web-services

我尝试使用ajax向网络服务拨打电话,以验证相框和密码是否正确。我只是在xml中返回pass或fail。在我的asmx页面中,我收到错误"非静态字段,方法或属性需要一个对象&system; web.web.ui.page.request.get"另外,我的xmlhttp.open网址,我做得对吗?有没有人建议如何解决这个问题?这是我的第一篇文章,如果我提出错误的提问或提供的信息不足,请告诉我。谢谢。

[WebMethod]
    public static string Auth() {
        String strConnString = ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString;
        string str = null;
        SqlCommand com;
        string query = String.Format("select COUNT(TeacherID) from USERS where User= '{0}' and Password='{1}'", Page.Request.QueryString["username"], Page.Request.QueryString["password"]);
        object obj = null;
        SqlConnection con = new SqlConnection(strConnString);
        con.Open();
        com = new SqlCommand(query, con);
        obj = com.ExecuteScalar();
        con.Close();
        Page.Response.Write(obj);
    }



 function getResult() {

        var xmlhttp = new XMLHttpRequest();
        xmlhttp.onreadystatechange = function () {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                document.getElementById("lblMessage").innerHTML = xmlhttp.responseText;
            }
        }
        xmlhttp.open("GET", "authenticate.asmx.cs?username=" + document.getElementById("txtUserName").value + "&password=" + document.getElementById("txtPassword").value, true);
        xmlhttp.send();

    }

1 个答案:

答案 0 :(得分:1)

"非静态字段,方法或属性&system; web.web.ui.page.request.get"需要一个对象 - 这是webservice的实际问题。解决了以下代码

HttpContext.Current.Request.QueryString["username"],   

HttpContext.Current.Request.QueryString["password"]);

而不是错过前缀的用户的上述发布行。

HttpContext.Current.

完整代码如下:

[WebMethod]
public static string Auth() {
    String strConnString = ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString;
    string str = null;
    SqlCommand com;
    string query = String.Format("select COUNT(TeacherID) from USERS where User= '{0}' and Password='{1}'",  HttpContext.Current.Request.QueryString["username"],  HttpContext.Current.Request.QueryString["password"]);
    object obj = null;
    SqlConnection con = new SqlConnection(strConnString);
    con.Open();
    com = new SqlCommand(query, con);
    obj = com.ExecuteScalar();
    con.Close();
    Page.Response.Write(obj);
}