探讨jQuery的ajax使用场景(c#)

2020-05-18 08:43:17易采站长站整理

TextJson.txt:是作为数据储存在文件中,让客户端来异步访问的。


response.aspx页面代码,response.aspx是:


<%@ Page Language=”C#” AutoEventWireup=”true” CodeBehind=”response.aspx.cs” Inherits=”JqueryAjax2.response” %>


没有任何html代码,因为主要是在服务器端获得客户端提交的数据,并返回数据给客户端,不需要显示html内容,所以可以不含html标记。
response.aspx.cs页面代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Runtime.Serialization.Json;
using System.Runtime.Serialization;


namespace JqueryAjax2
{
    public partial class response : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string str = Request[“key1”];
            Response.Write(“success” + str);
        }
    }
}

在代码中可以看到服务器端获得客户端提交的数据,并返回数据给客户端的方式。


test.htm静态页面的代码是:

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml” >
<head>
    <title></title>
</head>
<body>
test.html
</body>
</html>

当静态页面被请求后,会作为html的格式返回客户端,使用 $(“#resulthtml”).append(html);这样的方法来显示test.htm静态页面的内容。


TextJson.txt里面保存着一段文本,是json格式的:


{   “key1”: “刘明丰”,   “key2”: “林望”,   “key3”: “陈文杰”,   “key4”: “耿殿佳” }


在被访问后返回的是json格式的数据,在客户端获得json后会自动转换成对象。


好了,jQuery的异步使用场景基本满足我们的需求,自己试试看吧。