using System; using System.Collections.Generic; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace WebTest2008 { public partial class AjaxOperations : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!string.IsNullOrEmpty(Request["action"]) && Request["action"] == "jsonOp") // 处理JSON { string responseJsonTxt = ""; string tempStr = Request["sendStr"].Trim(new char[] { '{', '}' }); // 在服务器上解释JSON需要引用一个能够转化JSON的组件:Json.Net,这里简单测试,略过Json.Net if (tempStr.Split(new char[] { ',' })[0].Split(new char[] { ':' })[1] == "\"test\"" && tempStr.Split(new char[] { ',' })[1].Split(new char[] { ':' })[1] == "\"test\"") { responseJsonTxt = "{\"msg\":\"验证通过!\"}"; // 测试用 } else responseJsonTxt = "{\"msg\":\"验证失败!\"}"; Response.Write(responseJsonTxt); Response.End(); } }
jsTest.html引入json.js文件(必须下载json.js文件,否则js报错),如下:
<html> <head> <title>js test</title> <script src="https://www.jb51.net/js/json.js" type="text/javascript"></script> <script src="https://www.jb51.net/js/testJs.js" type="text/javascript"></script> </head> <body> <form> <div> 用户名:<input type="text" /> 密码:<input type="password" onblur="validatePwd(this)" /></div> </form> </body> </html>