MVC裡的ajax

來源:互聯網
上載者:User

標籤:

視圖介面代碼:

@{    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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.