weixin_33713707 2015-02-07 23:40 采纳率: 0%
浏览 18

Ajax调用Web服务

I'm trying to make a call using ajax to the web service to verify if a userame and password are correct. I am just returning pass or fail in xml. In my asmx page, I am getting an error "an object is required for the non static field , method, or property 'system.web.ui.page.request.get" Also, my xmlhttp.open URL, Am I doing it right ? Does anybody has a suggestion on how to solve this? This is my first post, please let me know if I have asked the question wrong or provided insufficient information. Thank you.

[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条回答 默认 最新

  • weixin_33682790 2015-02-08 00:28
    关注

    "an object is required for the non static field , method, or property 'system.web.ui.page.request.get"-- this was the actual problem for the webservice. Solved with putting the following code

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

    instead of the above posted lines by the user which missed the prefixes.

    HttpContext.Current.
    

    The full code is the following:

    [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);
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 u盘问题:盘符不显示 无媒体
  • ¥50 R语言读取nc按月均值转为tif
  • ¥30 智能车串级pid调参
  • ¥15 visual studio code翻译老是错误
  • ¥20 卫星测高数据的高程转换
  • ¥15 爬取招聘网站数据信息
  • ¥15 安装完tensorflow,import tensorflow as tf后报错,如何解决?
  • ¥15 ultralytics库导出onnx模型,模型失去预测能力
  • ¥15 linux下点对点协议连接2个USB串口的硬件流量控制问题
  • ¥15 SQL数据自动生成问题