初學ajax 一個簡單的功能,調試了2個小時,代碼如下雖然成功了 但是有錯誤
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title></title> <meta charset="utf-8" /> <link href="css/bootstrap.css" rel="stylesheet" /> <script src="scripts/jquery-1.11.3.min.js"></script> <script> $(function () { $("#btnLogin").click(function () { if ($("#txtName").val() == "") { alert("用戶名不能為空"); return false; } $.ajax({ type: 'POST', url: "Login.ashx", data: { userName: $("txtName").val(), userPwd: $("txtPwd").val() }, success: function (data) { alert(data); }, datatype:Text }) }) }) </script> </head> <body class="container"> <div class="group"> <label class="control-label">用戶名</label> <input id="txtName" class="form-control" name="txtName" /> </div> <div class="group"> <label class="control-label">密碼</label> <input id="txtPwd" class="form-control" name="txtPwd" /> </div> <button id="btnLogin" class="btn-group" name="btnLogin">登錄</button> <button id="btnReset" class="btn-group" name="btnReset">重置</button> </body> </html>
后台就是 新建 個一般處理程序 helloword
后來 dataType 換成 json 結果就不對了
在測試發現。。。json要用引號引起來
看文檔
dataType
類型:String
預期服務器返回的數據類型。如果不指定,jQuery 將自動根據 HTTP 包 MIME 信息來智能判斷,比如 XML MIME 類型就被識別為 XML。在 1.4 中,JSON 就會生成一個 JavaScript 對象,而 script 則會執行這個腳本。隨后服務器端返回的數據會根據這個值解析后,傳遞給回調函數。可用值:
- "xml": 返回 XML 文檔,可用 jQuery 處理。
- "html": 返回純文本 HTML 信息;包含的 script 標簽會在插入 dom 時執行。
- "script": 返回純文本 JavaScript 代碼。不會自動緩存結果。除非設置了 "cache" 參數。注意:在遠程請求時(不在同一個域下),所有 POST 請求都將轉為 GET 請求。(因為將使用 DOM 的 script標簽來加載)
- "json": 返回 JSON 數據 。
- "jsonp": JSONP 格式。使用 JSONP 形式調用函數時,如 "myurl?callback=?" jQuery 將自動替換 ? 為正確的函數名,以執行回調函數。
- "text": 返回純文本字符串
字符串。。。字符串。。。字符串 重要的事說三便 也就是 我前邊的text也是錯的 雖然 出來了正確的結果
再說第二個問題 傳過去后登錄 收不到值 。。。。又檢查 。。。。$("txtName")少個#號 。。這是一個id啊 這是一個id
最后前台成這樣了
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title></title> <meta charset="utf-8" /> <link href="css/bootstrap.css" rel="stylesheet" /> <script src="scripts/jquery-1.11.3.min.js"></script> <script> $(function () { $("#btnLogin").click(function () { if ($("#txtName").val() == "") { alert("用戶名不能為空"); return false; } var username = $("#txtName").val(); var userpwd =$("#txtPwd").val(); //$.ajax({ // type: 'POST', // url: "Login.ashx", // data: {userName: $("txtName").val(),userPwd: $("txtPwd").val() }, // success: function (data) { // alert(data); // } // //datatype:"json" //}) alert(username+userpwd); $.post("Login.ashx", { UserName:username, UserPass:userpwd }, function (result) { alert(result); }); }) }) </script> </head> <body class="container"> <div class="group"> <label class="control-label">用戶名</label> <input id="txtName" class="form-control" name="txtName" /> </div> <div class="group"> <label class="control-label">密碼</label> <input id="txtPwd" class="form-control" name="txtPwd" /> </div> <button id="btnLogin" class="btn-group" name="btnLogin">登錄</button> <button id="btnReset" class="btn-group" name="btnReset">重置</button> </body> </html>
.net 一般處理程序代碼 如下:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Data; using System.Data.SqlClient; namespace UI { /// <summary> /// Login 的摘要說明 /// </summary> public class Login : IHttpHandler { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; string userName = context.Request.Form["userName"]==null ?"":context.Request.Form["UserName"].ToString(); string userPwd = context.Request.Form["UserPass"] == null ?"" : context.Request.Form["UserPass"].ToString(); using (SqlConnection con = new SqlConnection("server =.;uid=sa;pwd=123;database=LT")) { using (SqlCommand cmd = new SqlCommand()) { string s = string.Format("select Count(1) cnt from users where userName ='{0}' and pwd='{1}'", userName, userPwd); cmd.CommandText = s; cmd.Connection = con; con.Open(); int cnt = int.Parse( cmd.ExecuteScalar().ToString()); if (cnt == 1) { context.Response.Write(userName+userPwd+"登錄成功"+s); } else { context.Response.Write(userName+ userPwd + "登錄失敗"+s); } } } } public bool IsReusable { get { return false; } } } }
一定要細心
但 有時由於以前的習慣,只是細心是不能解決的,這個需要靠長時間的積累了~~
要多做、多寫、多看。
