寫了一個腳本,如下: 1 $(function () { 2 $("#btnVcode").click(function () { 3 var receiveMobile = $("#Moblie").val();//手機號 4 var regMobile = /^1[3|4|5|7|8][0-9]
寫了一個腳本,如下:
1 $(function () { 2 $("#btnVcode").click(function () { 3 var receiveMobile = $("#Moblie").val();//手機號 4 var regMobile = /^1[3|4|5|7|8][0-9]\d{8}$/;//驗證手機 5 6 if (!regMobile.test(receiveMobile)) { 7 helper.dialogError('手機號格式不對'); 8 return; 9 } 10 $.post('/Register/SendSms', { Moblie: receiveMobile }, function (jsonObj) { 11 helper.checkAjaxStatus(jsonObj, function() { 13 }); 14 },'json'); 15 }); 16 });
手機號碼驗證通過後,會去後臺調用發送簡訊介面,如果簡訊發送成功,會創建session ,而session的原理有一點是要在客戶端瀏覽器創建cookie用來存ASP.NET_SessionId.
剛寫完這段代碼時擔心Ajax非同步請求不會創建本地cookie存sessionId. ~.~
因為後臺返回的是一個Json字元串: {"status":0,"msg":"簡訊已發送,請註意查收!"}
糾結了一會,想怎麼解決.
試試再說,經測試,竟然創建cookie了.(後臺沒有操作cookie,返回的結果是字元串,也沒有用jquery去操作去生成cookie,然而,然而,卻有了...有了...)
查看報文-->
POST http://localhost:1813/Register/SendSms HTTP/1.1
Host: localhost:1813
Connection: keep-alive
Content-Length: 18
Accept: application/json, text/javascript, */*; q=0.01
Origin: http://localhost:1813
X-Requested-With: XMLHttpRequest
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Referer: http://localhost:1813/Register/EpUser
Accept-Encoding: gzip, deflate
Accept-Language: zh-CN,zh;q=0.8
Moblie=***********
HTTP/1.1 200 OK
Cache-Control: private
Content-Type: application/json; charset=utf-8
Server: Microsoft-IIS/8.0
Set-Cookie: ASP.NET_SessionId=zbdy1ndgta2g02lysrfv1rhj; path=/; HttpOnly
X-AspNetMvc-Version: 4.0
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?ZDpcYmFja3VwXGRvY3VtZW50c1x2aXN1YWwgc3R1ZGlvIDIwMTNcUHJvamVjdHNcTFNVbmlvblxMU1VuaW9uLlNpdGVcUmVnaXN0ZXJcU2VuZFNtcw==?=
X-Powered-By: ASP.NET
Date: Fri, 29 Jan 2016 11:03:17 GMT
Content-Length: 53
{"status":0,"msg":"簡訊已發送,請註意查收!"}
真的有了.據分析,應該是
Session[Keys.smsVcode] = vcode;//簡訊發送成功才創建session
這行代碼會 自動創建一個cookie並放入響應流(類似 Response.Cookies.Add(...) ),體現在報文的頭部,瀏覽器收到報文,根據
Set-Cookie: ASP.NET_SessionId=zbdy1ndgta2g02lysrfv1rhj; path=/; HttpOnly
創建了cookie.
分析完畢.