您好,
I am trying to verify if a username is taken or available by
使用jquery向一个请求发送请求 asp.net页面。 ASP.net页面是 清楚地得到我的信息 我正在进入日志 数据库。我的SQL服务器数据库 确实显示了传递的用户名 它然后返回一个值 由于某种原因客户端 html / javascript没有回复它。 我不确定它是否已经结束了 我的html / javascript或者也许是我的 asp.net页面没有返回json 信息正确吗?这是我的 第一次尝试json
显示检查可用性框 在HTML页面上,但它永远不会改变 即使sql server显示它 运行存储过程
Html文件
$(document).ready(function () {
var validateUsername = $('#validateUsername');
$('#username').keyup(function () {
var t = this;
if (this.value != this.lastValue) {
if (this.timer) clearTimeout(this.timer);
validateUsername.removeClass('error').html('<img src="images/ajax-loader.gif" height="16" width="16" /> checking availability...');
this.timer = setTimeout(function () {
$.ajax({
url: 'ValidateUsername.aspx',
data: 'username=' + t.value,
dataType: 'json',
type: 'get',
success: function (j) {
validateUsername.html('HI!');
}
});
}, 200);
this.lastValue = this.value;
}
});
});
// - &GT;
用户名,有效:a-z。-_
asp.net page [CheckusernameAvailable.aspx]
<%@ Language="C#" AutoEventWireup="true" CodeBehind="CheckUsernameAvailable.aspx.cs" Inherits="Services_UsernameAvailable" %>
asp.net页面后面的Asp.net代码[CheckusernameAvailable.aspx.cs]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using UserSite.DataClasses;
using System.Data;
namespace OohruWeb
{
public partial class ValidateUsername : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.ContentType = "application/json";
string NameToLookUp = Request.QueryString["username"];
if (NameToLookUp == null) {
NameToLookUp = "";
}
DbaseExecSpWithReturnValue Sproc = new DbaseExecSpWithReturnValue();
Sproc.SetSp("sp_CheckUsernameAvailable");
Sproc.AddParam(1);
Sproc.AddParam("Username",SqlDbType.Char,NameToLookUp,20);
int RetVal = Sproc.Execute();
Sproc.Close();
if (RetVal == 0)
{
Response.Write(@"'{""success"": false}'");
}
if (RetVal == 1)
{
Response.Write(@"'{""success"": true}'");
}
}
}
}
答案 0 :(得分:1)
我相信您的问题是您需要使用[WebMethod]
属性和来装饰您的方法,需要对类进行修饰,以便将其暴露给客户端脚本。我通常使用网络服务文件.asmx
。
事实上,我不确定您是否可以使用常规类文件,但是创建一个Web服务文件并引用它使用的代码,并将其应用到您自己的文件中以查看它是否有效。
另外,我很惊讶您的代码正在运行,您的数据参数看起来不像json
格式。据我所知,应该是data: "{ }"
。
答案 1 :(得分:0)
尝试Response.Clear()
和Response.End()
,看看是否能解决您的问题。
protected void Page_Load(object sender, EventArgs e)
{
Response.Clear();
Response.ContentType = "application/json";
string NameToLookUp = Request.QueryString["username"];
if (NameToLookUp == null) {
NameToLookUp = "";
}
DbaseExecSpWithReturnValue Sproc = new DbaseExecSpWithReturnValue();
Sproc.SetSp("sp_CheckUsernameAvailable");
Sproc.AddParam(1);
Sproc.AddParam("Username",SqlDbType.Char,NameToLookUp,20);
int RetVal = Sproc.Execute();
Sproc.Close();
if (RetVal == 0)
{
Response.Write(@"'{""success"": false}'");
}
if (RetVal == 1)
{
Response.Write(@"'{""success"": true}'");
}
Response.End();
}
答案 2 :(得分:0)
试试这篇文章,这可能会让您想到使用JSON验证用户名
http://www.dotnetspark.com/kb/1278-check-username-availability-from-database.aspx