標籤:
視圖介面代碼:
@{ Layout = null;}<!DOCTYPE html><html><head> <meta name="viewport" content="width=device-width" /> <script src="~/js/jquery-1.7.1.min.js"></script> <title>Login</title></head><body> <div> 使用者名稱:<input type="text" id="uname" /><br /> 密碼:<input type="text" id="pwd" /><br /> <input type="button" value="登陸" id="btn1" /> </div></body></html><script type="text/javascript"> $("#btn1").click(function () { var a = $("#uname").val(); var b = $("#pwd").val(); $.ajax({ url: "/Ashxs/tttt.ashx", data: { "uname": a, "pwd": b }, type: "post", dataType: "json", success: function (data) { if (data.has == "1") { window.location.href = "Index"; } else { alert(‘使用者名稱密碼輸入有誤!‘); } }, error: function () { alert(‘伺服器串連失敗!‘); } }); });</script>
一般處理常式裡的代碼:
using System;using System.Collections.Generic;using System.Linq;using System.Web;using Mvc1.Models;using System.Collections;namespace Mvc1.Ashxs{ public class Login : IHttpHandler { Data0425DataContext con = new Data0425DataContext(); public void ProcessRequest(HttpContext context) { string uname = context.Request["uname"].ToString(); string pwd = context.Request["pwd"].ToString(); string end = ""; Users uu = con.Users.Where(r => r.UserName == uname && r.PassWord == pwd).FirstOrDefault(); if (uu != null) { end = "{\"has\":\"1\"}"; } else { end = "{\"has\":\"0\"}"; } context.Response.Write(end); } public bool IsReusable { get { return false; } } }}
控制器介面代碼:
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Mvc;using Mvc1.Models;namespace Mvc1.Controllers{ public class HomeController : Controller {public ActionResult Login() { return View(); } }}
MVC裡的ajax