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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵