Call databases through ajax, Html, and asp
I. Front-end page:
Test<Script src = "js/jquery-1.7.2.min.js" type = "text/javascript"> </script> <script type = "text/javascript"> function buttonClick () {var name = document. getElementById ("name "). value; var age = document. getElementById ("age "). value; var sex = document. getElementById ("sex "). value; alert ("name" + name + "age" + age + "Gender:" + sex); $. ajax ({type: "POST", url: "Test. ashx? Method = getLzr ", data: {name: name, age: age, sex: sex}, success: function (msg) {document. getElementById (" divTest "). innerHTML =""+ Msg +"
";}}) ;}</Script>
Name: |
|
Age: |
|
Gender: |
|
| Submit: |
Ii. ashx code:
Public class Handler1: IHttpHandler, IRequiresSessionState {HttpRequest Request; HttpResponse Response; public void ProcessRequest (HttpContext context) {Request = context. request; Response = context. response; string method = Request ["Method"]. toString (); // url parameter of the corresponding ajax method MethodInfo methodInfo = this. getType (). getMethod (method); // methodInfo. invoke (this, null);} public void getLzr () {string name = Request ["name"]. toString (); string age = Request ["age"]. toString (); string sex = Request ["sex"]. toString (); int I = SqlHelper. executeNonQuery ("Insert into lzr_name (name, age, sex) values (@ name, @ age, @ sex)", new SqlParameter ("@ name", name ), new SqlParameter ("@ age", age), new SqlParameter ("@ sex", sex); if (I> 0) {// Response data. write ("T");} else {Response. write ("F") ;}} public bool IsReusable {get {return false ;}}}
3. display: before clicking the button:
After clicking the button:
<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + pgltzybzcm9 "http://www.2cto.com/uploadfile/Collfiles/20140914/20140914092624107.png" alt = "">