[英]Ajax call to webservice
我正在嘗試使用Ajax調用Web服務以驗證userame和密碼是否正確。 我只是返回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();
}
“非靜態字段,方法或屬性'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);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.