开发者

validate username ajax and json and asp.net

Hello,

I am trying to verify if a username is taken or available by

using jquery to send a request to an asp.net page. The ASP.net page is clearly getting the information as I am having it make an entry into a log database. My SQL server database indeed shows the username being passed to it and is returning a value however for some reason the client side html/javascript doesn't RESPOND to it. I am not sure if it is on the end of my html/javascript or perhaps my asp.net page is not returning the json information correctly? This is my first attempt at json

The checking availability box shows up on the html page but it NEVER changes even when the sql server shows that it ran the stored procedure

Html File

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

//-->

Username, valid: a-z.-_

asp.net page [CheckusernameAvailable.aspx]

<%@  Language="C#"  AutoEventWireup="true" CodeBehind="CheckUsernameAvailable.aspx.cs"  Inherits="Services_UsernameAvailable" %>

Asp.net code behind asp.net page [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, Even开发者_高级运维tArgs 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}'");
            }
        }
    }
}


I believe your problem is that you need to decorate your method with the [WebMethod] attribute and the class needs to be decorated so that it is exposed to client side script. I usually use a web service file, .asmx.

In fact I'm not sure if you can use a regular class file like you have, but create a web service file and reference the code it uses, and apply it to your own file to see if it works.

Also, I'm surprised your code is working, your data parameter doesn't look like it's in json format. Should be data: "{ }" as far as I know.


Try Response.Clear() and Response.End() to see if that takes care of your problem.

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜