【AngularJS】通过jsonp与webmethod交互,实现ajax验证
服务端配置
1:新建一个asp.net的网站
2: 创建一个服务文件:LoginService.asmx
注意:记得取消[System.Web.Script.Services.ScriptService]的注释
因为要使用jsonp来达到跨域访问,所以要声明输出类型和加上callback函数前缀。
using System.Web.Services; namespace testService
{
/// <summary>
/// LoginService 的摘要说明
/// </summary>
[WebService(Namespace = "zhexian.com")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
[System.Web.Script.Services.ScriptService]
public class LoginService : System.Web.Services.WebService
{
[WebMethod]
public void CheckUserNameExist(string username)
{
Context.Response.ContentType = "application/x-javascript";
string result = username == "kimmy" ? "true" : "false";
string callBackName = Context.Request.QueryString["callback"];
Context.Response.Write(callBackName + "({\"isUnique\":\"" + result + "\"})");
}
}
}
3:配置web.config
在web.config, <system.web>下,加入字段
<webServices>
<protocols>
<add name="HttpPost"/>
<add name="HttpGet"/>
</protocols>
</webServices>
网页配置
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>入门入门</title>
<link rel="stylesheet" href="http://cdn.jsdelivr.net/foundation/4.3.2/css/foundation.min.css">
<style type="text/css">
</style>
</head>
<body ng-app="myApp">
<form name="singnupForm" novalidate ng-submit="signupForm()">
<fieldset>
<legend>注册</legend>
<div> <div class="row">
<div class="large-12 columns"> <input type="text" placeholder="用户名" name="name" ng-model="user.name" ng-minlength="2" ng-maxlength="20" ensure-unique="name" required /> <div class="error" ng-show="singnupForm.name.$dirty && singnupForm.name.$invalid">
<small class="error" ng-show="singnupForm.name.$error.required">
请录入用户名
</small>
<small class="error" ng-show="singnupForm.name.$error.minlength">
最少2个字符
</small>
<small class="error" ng-show="singnupForm.name.$error.maxlength">
最多20个字符
</small>
<small class="error" ng-show="singnupForm.name.$error.unique">
用户名已经存在
</small>
</div>
</div>
</div> <div class="row">
<div class="large-12 columns">
<input type="password" placeholder="密码" name="pwd" ng-model="user.pwd" ng-minlength="5" required />
<div class="error" ng-show="singnupForm.pwd.$dirty && singnupForm.pwd.$invalid">
<small class="error" ng-show="singnupForm.pwd.$error.required">
请录入密码
</small>
<small class="error" ng-show="singnupForm.pwd.$error.minlength">
最少5个字符
</small>
</div>
</div>
</div> <div class="row">
<div class="large-12 columns">
<input type="email" placeholder="邮件地址" name="email" ng-model="user.email" ng-minlength="5" ng-maxlength="20" required />
<div class="error" ng-show="singnupForm.email.$dirty && singnupForm.email.$invalid">
<small clss="error" ng-show="singnupForm.email.$error.required">
请录入邮件地址
</small>
<small class="error" ng-show="singnupForm.email.$error.email">
邮件地址不正确
</small>
<small class="error" ng-show="singnupForm.email.$error.minlength">
最少3个字符
</small>
<small class="error" ng-show="singnupForm.email.$error.maxlength">
最长20个字符
</small>
</div>
</div>
</div> <div class="row">
<div class="large-12 columns">
<input type="number" placeholder="年龄" name="age" ng-model="user.age" ng-minlength="1" required />
<div class="error" ng-show="singnupForm.age.$dirty && singnupForm.age.$invalid">
<small clss="error" ng-show="singnupForm.age.$error.required">
请录入年龄
</small>
<small class="error" ng-show="singnupForm.age.$error.number">
年龄格式不正确
</small>
</div>
<button type="submit" ng-disabled="singnupForm.$invalid" class="button radius">submit</button>
</div>
</div> </fieldset>
</form>
<script src="angular.min.js"></script>
</body>
</html>
JS部分,加在Body前面
<script type="text/javascript">
var myApp = angular.module('myApp',[]); myApp.directive('ensureUnique',function ($http) { return {
require:'ngModel',
link:function(scope,ele,attrs,c){
scope.$watch(attrs.ngModel,function (n) { if (!n)
return; $http.jsonp(
'http://192.168.18.114:3301/LoginService.asmx/CheckUserNameExist?callback=JSON_CALLBACK',
{
params :{'username':scope.user.name}
}
).success(function (data) {
var isUnique = data && data.isUnique && data.isUnique.toLowerCase()=="true"; c.$setValidity('unique',isUnique);
}).error(function (data) { c.$setValidity('unique',false);
});
});
}
};
});
</script>
【AngularJS】通过jsonp与webmethod交互,实现ajax验证的更多相关文章
- 【AngularJs】---JSONP跨域访问数据传输
大家会自然想到只有一个字母之差的JSON吧~ JSON(JavaScript Object Notation)和JSONP(JSON with Padding)虽然只有一个字母的差别,但其实他们根本不 ...
- 【AngularJs】---JSONP跨域访问数据传输(JSON_CALLBACK)
大家会自然想到只有一个字母之差的JSON吧~ JSON(JavaScript Object Notation)和JSONP(JSON with Padding)虽然只有一个字母的差别,但其实他们根本不 ...
- 一、Django前后端交互之Ajax和跨域问题
一.Ajax介绍 1.概述 AJAX 是一种在无需重新加载整个网页的情况下,能够更新部分网页的技术.AJAX(Asynchronous Javascript And XML)翻译成中文就是“异步Jav ...
- AngularJs与Java Web服务器交互
AngularJs是Google工程师研发的产品,它的强大之处不是几句话就能描述的,只有真正使用过的人才能体会到,笔者准备在这篇文章中,以一个简单的登录校验的例子说明如何使用AngularJs和Web ...
- 无废话ExtJs 入门教程二十[数据交互:AJAX]
无废话ExtJs 入门教程二十[数据交互:AJAX] extjs技术交流,欢迎加群(521711109) 1.代码如下: 1 <!DOCTYPE html PUBLIC "-//W3C ...
- jsonp其实很简单【ajax跨域请求】
js便签笔记(13)——jsonp其实很简单[ajax跨域请求] 前两天被问到ajax跨域如何解决,还真被问住了,光知道有个什么jsonp,迷迷糊糊的没有说上来.抱着有问题必须解决的态度,我看了许多资 ...
- 基于JS的ajax的实现,JSON和JSONP,基于JQuery的ajax的实现
基于JS的ajax的实现,JSON和JSONP,基于JQuery的ajax的实现1.基于JS的ajax的实现 step1: var xmlhttp = XMLHttprequest() #实例化一个对 ...
- ajax验证表单元素规范正确与否 ajax展示加载数据库数据 ajax三级联动
一.ajax验证表单元素规范正确与否 以用ajax来验证用户名是否被占用为例 1创建表单元素<input type="text" id="t"> 2 ...
- 利用jQuery实现的Ajax 验证用户名是否存在
异步刷新实现方式有多种,也可以借助JS的多种框架,下面是使用jQuery框架实现的AJAX 验证用户名是否存在 jQuery.ajax概述 HTTP 请求加载远程数据. 通过jQuery 底层 AJA ...
随机推荐
- 判断ActiveX控件是Desgin Mode还是Runtime Mode
对于MFC COleControl::AmbientUserMode Determines if the Container is in design mode or user mode. BOOL ...
- Spring学习笔记--自动检测
要使用自动检测,我们需要用到<context:annotation-scan>标签.<context:annotation-scan>元素除了完成与<context:an ...
- dubbo项目实战代码展示
最近公司项目使用dubbo服务,于是就去网上搜索关于dubbo的相关资料,真的很多,但是对于很多人并不是很了解框架或者 不是太适合新手的片段代码,于是我就根据项目的相关内容把dubbo部分单独切出来, ...
- 移动App如何实践单点登录
移动应用实现单点登录,分析下面两种需求: 需求一: 账号在正在设备A上使用(应用是设备A上当前活动应用), 若设备B上用该账号登录应用,要求设备A上的账号立即退出. 解决办法: 应用客户端上做个定时 ...
- 1855: [Scoi2010]股票交易[单调队列优化DP]
1855: [Scoi2010]股票交易 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 1083 Solved: 519[Submit][Status] ...
- zookeeper报错Will not attempt to authenticate using SASL (unknown error)
Will not attempt to authenticate using SASL (unknown error) 转自:http://blog.csdn.net/mo_xingwang/arti ...
- java URL、HTTP与HTML+CSS
一.Web三大基石 1 二.API(Application Programming Interface,应用程序编程接口) 1 三.题目分析总结: 3 五.HTTP协议与寄信是类似的 6 请求报文 6 ...
- Struts2+Spring3+MyBatis3整合以及Spring注解开发
分类: Web(2) 版权声明:本文为博主原创文章,未经博主允许不得转载. 最近在做一个SpringMVC+spring+MyBatis的项目,突然想起以前自己要搭建一个Struts2+Sprin ...
- intelij IDEA在启动tomcat时控制台日志乱码
1.在idea安装目录的bin下修改idea.exe.vmoptions和idea64.exe.vmoptions,添加 -Dfile.encoding=UTF-8 -Dconsole.encodin ...
- 剑指Offer——和为S的连续正数序列
题目描述: 小明很喜欢数学,有一天他在做数学作业时,要求计算出9~16的和,他马上就写出了正确答案是100.但是他并不满足于此,他在想究竟有多少种连续的正数序列的和为100(至少包括两个数).没多久, ...