Jquery Ajax asynchronous request session, jqueryajax
A script is written as follows:
1 $ (function () {2 $ ("# btnVcode "). click (function () {3 var receiveMobile =$ ("# Moblie "). val (); // mobile phone number 4 var regMobile =/^ 1 [3 | 4 | 5 | 7 | 8] [0-9] \ d {8} $ /; // verify mobile phone 5 6 if (! RegMobile. test (receiveMobile) {7 helper. dialogError ('incorrect Mobile Phone Number Format '); 8 return; 9} 10 $. post ('/Register/SendSms', {Moblie: receiveMobile}, function (jsonObj) {11 helper. checkAjaxStatus (jsonObj, function () {13}); 14}, 'json'); 15}); 16 });
After the mobile phone number verification is passed, the system will go to the background to call the interface for sending text messages. If the text message is successfully sent, a session will be created. The principle of session is to create a cookie in the client browser to store ASP. NET_SessionId.
When writing this code, I was worried that the asynchronous Ajax request would not create a local cookie to store sessionId .~.~
Because the background returns a Json string: {"status": 0, "msg": "The text message has been sent. Please check it! "}
After a while, I want to solve it.
After testing, we created a cookie. (No cookie operation in the background, the returned result is a string, and jquery is not used to generate a cookie. However, there is... with ...)
View packets -->
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-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? Rows =? =
X-Powered-By: ASP. NET
Date: Fri, 29 Jan 2016 11:03:17 GMT
Content-Length: 53
{"Status": 0, "msg": "The SMS has been sent. Check the message! "}
Actually, according to the analysis, it should be
Session [Keys. smsVcode] = vcode; // The session is created only when the message is sent successfully.
This line of code will automatically create a cookie and put it into the Response stream (similar to Response. Cookies. Add (...), reflected in the header of the message, the browser receives the packet, according
Set-Cookie: ASP. NET_SessionId = zbdy1ndgta2g02lysrfv1rhj; path =/; HttpOnly
Cookie is created.
Analysis completed.