【问题标题】:Ajax call to webservice对 Web 服务的 Ajax 调用
【发布时间】:2015-02-07 23:40:15
【问题描述】:

我正在尝试使用 ajax 调用 Web 服务以验证用户名和密码是否正确。我只是在 xml 中返回通过或失败。在我的 asmx 页面中,我收到一个错误“非静态字段、方法或属性‘system.web.ui.page.request.get’需要一个对象”另外,我的 xmlhttp.open URL,我在做吗正确的 ?有人对如何解决这个问题有建议吗?这是我的第一篇文章,如果我问错了问题或提供的信息不足,请告诉我。谢谢你。

[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();

    }

【问题讨论】:

  • 您是否得到任何结果、任何错误,请提供更多信息。
  • 如果我将该方法放在我的 aspx.cs 页面的页面加载中,它可以工作,返回 1 或 0,但是当我将它放在我的 Web 服务中时,我得到“需要一个对象对于非静态字段、方法或属性“system.web.ui.page.request.get”,下划线错误来自 Page.Request。和 page.Response
  • 使用 HttpContext.Current.Request.QueryString 看看是否有帮助...
  • Esc,谢谢!我将阅读有关 httpcontext 的更多信息,最后一件事,我的 xmlHttp.open URL,我无法让它工作,但 Web 服务可以工作。另外,tempuri 命名空间,我会放什么?
  • 请对第二个问题更准确一些,HttpContext.Current.Request.QueryString 解决了您的问题吗?

标签: c# ajax xml web-services


【解决方案1】:

“非静态字段、方法或属性‘system.web.ui.page.request.get’需要一个对象”——这是网络服务的实际问题。用下面的代码解决了

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);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-29
    • 2013-06-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多