验证用户名ajax,json和asp.net [英] validate username ajax and json and asp.net
问题描述
所以我是json/ajax/jquery/webservices的新手
So I'm new to json/ajax/jquery/webservices
我正在尝试创建一个联接页面,以便用户输入几个字母并获得有关用户名是否可用的即时反馈.
I am trying to do a join page whereby the user types in a few letters and gets instant feedback on whether or not the username is available or not.
我有一个html文件[用户前端]和一个asmx网络服务,该网络服务会检查是否存在任何文本[它运行一个运行良好的存储过程]
I have an html file [the user front end], and an asmx webservice, the webservice checks to see if whatever text it has gotten exists [it runs a stored procedure which works fine]
我的html文件似乎不是要调用我的Web服务.
My html file doesn't seem be to calling my webservice.
我已经验证了Web服务的工作原理,方法是转到asmx页并手动输入值,它为我返回true或false.
一个问题是,它似乎正在以XML形式返回,如下所示,而不是原来的json
I have verified the webservice works by going to the asmx page and manually and typing in the value and it returns a true or false for me.
One problem is it seems to be returning it in XML as seen below instead of the json I was it to be in
<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://localhost">{"available": false}</string>
接下来是代码/标记!
我的html文件
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
<script type="text/javascript">
<!--
$(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({
contentType: "application/json; charset=utf-8",
url: 'ValidateUser.asmx/GetUsernameAvailable',
data: '{username: "'+t.value + '"}',
dataType: 'json',
type: "post",
success: function (j) {
validateUsername.html('I willl have my logic in here for changing the html on the label to reflect success or failure!');
}
});
}, 200);
this.lastValue = this.value;
}
});
});
//-->
</script>
</head>
<body>
<fieldset>
<div>
<label for="username">Username, valid: a-z.-_</label>
<input type="text" name="username" value="" id="username" />
<span id="validateUsername"></span>
</div>
</fieldset>
</body>
</html>
我的asmx文件
<%@ WebService Language="C#" Class="ValidateUser" %>
using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Data;
using System.Data.Odbc;
using System.Web.Script.Serialization;
using System.Web.Script.Services;
using UserSite.DataClasses;
[WebService(Description = "Web services to query Username availability.", Namespace = "http://localhost")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
public class ValidateUser: System.Web.Services.WebService
{
[WebMethod(Description = "Gets the availability of a username.")]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string GetUsernameAvailable(string username)
{
if (username == null)
{
username = "";
}
DbaseExecSpWithReturnValue Sproc = new DbaseExecSpWithReturnValue();
Sproc.SetSp("sp_CheckUsernameAvailable");
Sproc.AddParam(1);
Sproc.AddParam("Username", SqlDbType.Char, username, 20);
int RetVal = Sproc.Execute();
Sproc.Close();
if (RetVal == 0)
{
return @"{""available"": false}";
}
else
{
return @"{""available"": true}";
}
}
}
因此,存储过程的运行方式与我手动运行asmx文件时看到的一样,但是html页面未调用它,并且我的asmx文件未返回数据.....因此,基本上它使我发疯了!
So the Stored Procedure works as I saw when I manually ran the asmx file but the html page is not calling it and my asmx file is not returning the data..... so basically Its driving me nuts!
推荐答案
您正在使用哪个版本的.NET-如果为3.5,请确保已在web.config中注册了ScriptHandlerFactory和ScriptModule-这些负责处理JSON请求
Which version of .NET are you using - if 3.5 then make sure that you have ScriptHandlerFactory and ScriptModule registered in web.config - these are responsible for handling JSON requests.
第二个问题是,在实际的服务实现中,应该返回所需的对象,并且ASP.NET基础结构将处理JSON序列化-您不必输出实际的JSON数据.例如,
Second issue is that in the actual service implementation, you are supposed to return the needed object and ASP.NET infrastructure will handle JSON serialization - you don't have to output actual JSON data. For example,
public bool GetUsernameAvailable(string username)
{
...
return (RetVal == 0) ? false : true;
}
以上将返回布尔值,您可以在调用函数中以j.d
的形式调用access.例如,
Above would return boolean value and you call access it in your call function as j.d
. For example,
...
$ajax({
...
success: function (j) {
alert(j.d); // will alert either true or false
}
...
最后,在浏览器中导航到asmx端点将调用soap端点,并且您将始终获得xml request-response(这是因为ASP.NET脚本处理程序仅在request为POST request并且content-type为JSON).调试此错误的正确方法是在Fiddler之类的工具中检查服务调用.
Lastly, navigating to asmx endpoint in browser will invoke soap end-point and you will always get xml request-response (this is because ASP.NET script handler will do JSON serialization only if request is POST request and content-type is JSON). Correct way to debug this will be to check you service call in tool such as Fiddler.
这篇关于验证用户名ajax,json和asp.net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!