ajax数据传输方式实例详解

2019-09-14 07:02:12王冬梅

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="js/json.js" type="text/javascript"></script>
 <script src="js/testJs.js" type="text/javascript"></script>
</head>
<body>
 <form id="form1">
 <div>
 用户名:<input id="txtUserName" name="txtUserName" type="text" />
  密码:<input id="txtPwd" name="txtPwd" type="password" onblur="validatePwd(this)" /></div>
 </form>
</body>
</html>

希望本文所述对大家ajax程序设计有所帮助。