登录页面使用ASP.Net和放大器;阿贾克斯 [英] Login Page using ASP.Net & Ajax
问题描述
我想用做一个登录页面的HTML,AJAX和放大器; ASP.NET.The数据是真正传递给Ajax功能,但是当我调试ASP页面的用户名和密码发送带有NULL。 在code应该采取用户名和放大器;密码,然后返回用户ID
I'm trying to make a login page using html, ajax & ASP.NET.The data is truly passed to the ajax function, but when I debug the asp page the username and password are sent with NULL. The code is supposed to take username & password then returns the userid
Html页面:
<div id="usernameid">Username:</div><input id="username" type="text"/> <span id="username_status"></span>
<div id="passwordid">Password:</div><input id="password" type="password"/> <span id="password_status"></span>
<div> <input id="loginbutton" onclick="UserLogin()" type="submit" value="Submit" /></div>
JavaScript的:
Javascript:
function UserLogin() {
var postData = JSON.stringify({ "username": JSON.stringify($("#username").val()), "password": JSON.stringify($("#password").val()) });
alert(postData);
$.ajax({
type: "GET",
url: "http://localhost:49317/LoginPageForLearn.aspx",
data: postData,
contentType: "application/json; charset=utf-8",
dataType: "jsonp",
jsonp: 'jsoncallback',
success: callbackfunction,
error: function (msg) { alert(msg); }
});
}
function callbackfunction(datacoming) {
localStorage["UserID"] = datacoming;
alert(datacoming);
Asp.net页面:
Asp.net page:
protected void Page_Load(object sender, EventArgs e)
{
string userName = "";
int userId = -1;
string PassWord = "";
if (Request.QueryString.Count != 0 && Request.QueryString["username"] != string.Empty && Request.QueryString["password"] != string.Empty)
{
userName = Request.QueryString["username"];
PassWord = Request.QueryString["password"];
userId = GetUserID(userName, PassWord);
}
}
你有,为什么不将数据正确地传递什么想法?还是你对我怎样才能使一个登录页面,使用HTML的任何其他的想法,并在SQL访问数据。
Do you have any ideas why isn't the data passed correctly ? Or do you have any other ideas on how can I make a login page using html and access the data at SQL.
非常感谢。
推荐答案
您可以创建一个PageMethod的:
You could create a PageMethod:
[WebMethod]
public static bool MyMethod(string username, string password)
{
...
return true;
}
,然后摆脱双JSON字符串化和调用页面的方法:
and then get rid of the double JSON stringification and call the page method:
var postData = JSON.stringify({
"username": $("#username").val(),
"password": $("#password").val()
});
$.ajax({
type: "POST",
url: "LoginPageForLearn.aspx/MyMethod",
data: postData,
contentType: "application/json; charset=utf-8",
success: callbackfunction,
error: function (msg) { alert(msg); }
});
和在回调,你可以测试页面方法的结果是:
and in your callback you could test the result of the page method:
function callbackfunction(result) {
if (result.d) {
alert('success');
}
}
这篇关于登录页面使用ASP.Net和放大器;阿贾克斯的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!