An example of understanding Ajax, JSON, and ashx is that when you enter the user name, it is very simple to compare the request page. However, it allows beginners to better understand Ajax in query.
Javascript: Javascript Code
-
- <SCRIPT src = "JS/jquery-1.4.2.js" type = "text/JavaScript"> </SCRIPT>
-
- <SCRIPT type ="Text/JavaScript">
-
- Function alertmsg ()
-
- {
-
- VaR user = {"ID": "", "PWD ":""};
-
- User. ID = $ ("Input: Text"). Val ();
-
- User. Pwd = $ ("Input: Password"). Val ();
-
- $. Ajax ({
- URL:"Handleajax. ashx", // we recommend that you use ashx to process the request page.ProgramIt is equivalent to a lightweight Aspx. Here there is no need to use aspx for data processing, which makes a little contribution to performance.
-
- Data:"User =" + User. ID,
-
- Datatype:"JSON", // data type returned on the request page
-
- Type:"Get ",
-
- Contenttype:"Application/JSON", // note that the contenttype of the request page must be consistent here
- Success:Function (data) {// The data here is the data returned by the request page
-
- Alert (data. Result );
-
- },
-
- Error:Function (XMLHttpRequest, textstatus, errorthrown ){
-
- Alert ("XMLHttpRequest ");
- }
-
- });
-
-
- }
<SCRIPT src = "JS/jquery-1.4.2.js" type = "text/JavaScript"> </SCRIPT> <SCRIPT type = "text/JavaScript"> function alertmsg () {var user = {"ID": "", "PWD": ""}; user. id = $ ("input: Text "). val (); User. pwd = $ ("input: Password "). val (); $. ajax ({URL: "handleajax. ashx ", // we recommend that you use the ashx processing program on the request page. The general processing program is equivalent to a lightweight Aspx. There is no need to use aspx for data processing, it makes a little contribution to the performance. Data: "user =" + User. ID, datatype: "JSON", // type returned by the request page: "Get", contenttype: "application/JSON ", // note that the contenttype of the request page must be consistent here success: function (data) {// The data here is the data returned by the request page alert (data. result) ;}, error: function (XMLHttpRequest, textstatus, errorthrown) {alert ("XMLHttpRequest ");}});}
HTML: HTML code
- <Body>
- User name:<Input id = "ID" type = "text" name = "ID"/>
- Password:<Input id = "PWD" type = "password" name = "PWD"/>
- <Input id = "Submit" type = "button" name = "sub" value = "Submit" onclick = "alertmsg ();"/>
- </Body>
<Body> User name: <input id = "ID" type = "text" name = "ID"/> password: <input id = "PWD" type = "password" name = "PWD"/> <input id = "Submit" type = "button" name = "sub" value = "Submit "onclick =" alertmsg (); "/> </body>
Request page ashx C # code
-
- <% @ Webhandler Language = "C #" class = "handleajax" %>
-
-
- Using system;
-
- Using system. Web;
-
-
- Public class handleajax: ihttphandler {
-
-
- Public void processrequest (httpcontext context ){
-
- Context. response. contenttype ="Application/JSON ";
-
- If (context. request ["user"] = "JiaYou ")
- {
-
- String Hello = "OK ";
-
- Context. response. Write ("{\" Result \ ": \" "+ Hello +" \ "}"); // note the format "key ": "value" The format here is correct only after I have written the error several times
-
- }
-
- Else {context. response. Write ("{\" Result \ ": \" Hello pig \"");}
-
- }
-
-
- Public bool isreusable {
- Get {
-
- Return false;
-
- }
-
- }
-
- }