问题描述
ashx文件如下<%@WebHandlerLanguage="C#"Class="SendPhoneCheck"%>usingSystem;usingSystem.Web;usingSystem.Collections.Generic;usingSystem.Data;usingDX.YYDai.Bill;usingDX.YYDAI.DBModel;usingDX.Foundation.Entity;usingDX.ERP.Common.Parm;usingDX.Foundation.Utils.Logger;publicclassSendPhoneCheck:IHttpHandler{stringphoneNo=string.Empty;publicstaticstringDateTimeFormat="yyyy'-'MM'-'dd'T'HH':'mm':'ss";tb_phoneNoCheckLogiclogic=newtb_phoneNoCheckLogic();publicvoidProcessRequest(HttpContextcontext){context.Response.ContentType="text/plain";InitData(context);context.Response.Write(GetJsonStr());}privatevoidInitData(HttpContextcontext){phoneNo=context.Request["phoneNo"];//传入}publicIDictionary<string,object>GetDictData(){IDictionary<string,object>obj=newDictionary<string,object>();try{if(string.IsNullOrEmpty(phoneNo))thrownewException("手机号码不能为空!");tb_phoneNoCheckdata=logic.GetPhoneNoCheck(phoneNo);tb_userInfoLogicuserInfoLogic=newtb_userInfoLogic();tb_userInfouserinfo=userInfoLogic.GetUserInfoByUserName(phoneNo);if(userinfo!=null)thrownewException("该手机号码已注册,请更换手机号码");if(null==data){stringrandNo=PubTools.GenerateCheckCode();data=newtb_phoneNoCheck();data.phoneNo=phoneNo;data.CheckNo=randNo;data.OrderDate=DateTime.Now;logic.Insert(data);//增加验证码记录PubTools.SendMessage(phoneNo,randNo);//发送手机验证码}else{stringrandNo=PubTools.GenerateCheckCode();data.CheckNo=randNo;data.OrderDate=DateTime.Now;logic.Update(data,data.CheckID.Value);PubTools.SendMessage(phoneNo,randNo);}AddDict(obj,1,"验证码发送成功!");returnobj;}catch(Exceptionex){newLogger("SendPhoneCheck.ashx").Exp(ex);AddDict(obj,0,ex.Message);returnobj;}}publicstaticvoidAddDict(IDictionary<string,object>obj,intflag,stringmessage){obj.Add("flag",flag);obj.Add("message",message);}///<summary>///获取数据模型///</summary>///<returns></returns>publicstringGetJsonStr(){IDictionary<string,object>obj=newDictionary<string,object>();try{obj=GetDictData();}catch(Exceptionex){AddDict(obj,0,"验证码发送失败");//newDX.Foundation.Utils.Logger.Logger("MyShowService.ashx").Exp(ex);//jsonTools.AddDict(obj,jsonTools.STATUS_ERROR,0,jsonTools.MESSAGE_ERROR,null);}stringstr=Encode(obj);returnstr;}publicboolIsReusable{get{returnfalse;}}publicstaticstringEncode(objecto){if(o==null||o.ToString()=="null")returnnull;if(o!=null&&(o.GetType()==typeof(String)||o.GetType()==typeof(string))){returno.ToString();}Newtonsoft.Json.Converters.IsoDateTimeConverterdt=newNewtonsoft.Json.Converters.IsoDateTimeConverter();dt.DateTimeFormat=DateTimeFormat;returnNewtonsoft.Json.JsonConvert.SerializeObject(o,dt);}}
应该如何写JS才能正确的把phoneNo提交进去并且得到flag和message两个参数呢?
解决方案
解决方案二:
直接用http://xxx.ashx?phoneNo=13900000000打开网址就行,至于获取结果,下面是参考示例:functionajax_submit(method,url,callback){varxmlhttp;try{xmlhttp=newXMLHttpRequest();}catch(e){xmlhttp=newActiveXObject("Microsoft.XMLHTTP");}xmlhttp.onreadystatechange=function(){if(4==xmlhttp.readyState&&200==xmlhttp.status)callback(xmlhttp);}xmlhttp.open(method,url,true);xmlhttp.setRequestHeader('Content-type','application/x-www-form-urlencoded');xmlhttp.send("");}functionset_data(xmlhttp){//varxml=xmlhttp.responseXML;//alert(xml.documentElement.childNodes[0].text);eval("xx="+xmlhttp.responseText);alert(xx.flag);alert(xx.message);}ajax_submit("get","http://localhost:58158/Web/test.ashx?p=123",set_data);
解决方案三:
url传参数就行了吧
解决方案四:
就一个手机号码验证,要那么复杂的什么json啊,什么Dictionary啊,不就是一个get传递参数吗?同时设定cache为false,否则你需要一个随机数让浏览器不记录你的html缓存