2015-02-07 154 views
1

我正在嘗試使用ajax調用Web服務來驗證用戶名和密碼是否正確。我只是在xml中返回通過或失敗。在我的asmx頁面中,我收到一個錯誤「一個對象是非靜態字段,方法或屬性'system.web.ui.page.request.get」所必需的。另外,我的xmlhttp.open URL,我在做對 ?有人有關於如何解決這個問題的建議嗎?這是我的第一篇文章,如果我提問或提供的信息不足,請告訴我。謝謝。Ajax調用webservice

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

    } 
+0

,能得到任何結果,任何錯誤,請提供更多信息。 – 2015-02-07 23:47:20

+0

如果我把這個方法放在我的aspx.cs頁面的頁面加載中,它工作,返回1或0,但是當我把它放在我的web服務中時,我得到了「一個對象是非靜態字段需要的,方法,或屬性'system.web.ui.page.request.get「,下劃線錯誤來自Page.Request。和page.Response – CompressedAir 2015-02-07 23:49:22

+0

使用HttpContext.Current.Request.QueryString,看看它是否有幫助... – 2015-02-07 23:57:33

回答

1

「的對象是所必需的非靜態字段,方法或屬性「system.web.ui.page.request.get」 - 這是對web服務的實際問題。通過輸入以下代碼來解決:

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