C# 对webservice的Ajax调用
我试图使用ajax调用web服务,以验证用户名和密码是否正确。我只是返回xml中的通过或失败。在我的asmx页面中,我收到一个错误“非静态字段、方法或属性'system.web.ui.page.request.get'需要一个对象”还有,我的xmlhttp.open URL,我做得对吗?有人对如何解决这个问题有什么建议吗?这是我的第一篇帖子,如果我问错了问题或者提供的信息不足,请告诉我。多谢各位C# 对webservice的Ajax调用,c#,ajax,xml,web-services,C#,Ajax,Xml,Web Services,我试图使用ajax调用web服务,以验证用户名和密码是否正确。我只是返回xml中的通过或失败。在我的asmx页面中,我收到一个错误“非静态字段、方法或属性'system.web.ui.page.request.get'需要一个对象”还有,我的xmlhttp.open URL,我做得对吗?有人对如何解决这个问题有什么建议吗?这是我的第一篇帖子,如果我问错了问题或者提供的信息不足,请告诉我。多谢各位 [WebMethod] public static string Auth() {
[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'需要对象”--这是webservice的实际问题。通过放置以下代码来解决
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);
}
您是否收到任何结果或错误,请提供更多信息。如果我将该方法放入aspx.cs页面的页面加载中,它会工作,返回1或0,但当我将其放入web服务中时,我得到“非静态字段、方法或属性'system.web.ui.page.request.get'需要对象”下划线错误来自Page.Request。和page.Responseuse HttpContext.Current.Request.QueryString,看看是否有帮助…Esc,谢谢!我将阅读更多关于httpcontext的内容,还有最后一件事,我的xmlHttp.openURL,我无法让它工作,但web服务可以工作。还有,tempuri名称空间,我应该放在那里什么?请更准确地回答第二个问题,HttpContext.Current.Request.QueryString是否解决了您的问题?