1. 导入等下我们要使用的文件AjaxMsgHelper.cs和DataHelper.cs他们的代码如下

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WebApplication1
{
/// <summary>
/// 生成 Ajax 响应消息 字符串
/// 数据格式:{"statu":"err","msg":"出错啦~~","data":[{},{}],"nextUrl":"Login.aspx"}
/// </summary>
public static class AjaxMsgHelper
{
#region 1.0 生成 Ajax消息 字符串 +static string AjaxMsg
/// <summary>
/// 生成 Ajax消息 字符串
/// </summary>
/// <param name="statu"></param>
/// <param name="msg"></param>
/// <returns></returns>
public static void AjaxMsg(string statu, string msg)
{
AjaxMsgHelper.AjaxMsg(statu, msg, "null", "null");
}
/// <summary>
/// 生成 Ajax消息 字符串
/// </summary>
/// <param name="statu"></param>
/// <param name="msg"></param>
/// <param name="data"></param>
/// <returns></returns>
public static void AjaxMsg(string statu, string msg, string data)
{
AjaxMsgHelper.AjaxMsg(statu, msg, data, "null");
}
/// <summary>
/// 生成 Ajax消息 字符串
/// </summary>
/// <param name="statu"></param>
/// <param name="msg"></param>
/// <param name="data"></param>
/// <param name="nextUrl"></param>
/// <returns></returns>
public static void AjaxMsg(string statu, string msg, string data, string nextUrl)
{
//{"statu":"err","msg":"出错啦~~","data":[{},{}],"nextUrl":"Login.aspx"}
string strMsg = "{\"statu\":\"" + statu + "\",\"msg\":\"" + msg + "\",\"data\":" + data + ",\"nextUrl\":\"" + nextUrl + "\"}";
//直接输出 数据 到 浏览器
System.Web.HttpContext.Current.Response.Write(strMsg);
}
#endregion
}
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Script.Serialization;

namespace WebApplication1
{
public static class DataHelper
{
/// <summary>
/// js 序列化器
/// </summary>
static JavaScriptSerializer jss = new JavaScriptSerializer();

/// <summary>
/// 将 对象 转成 json格式字符串
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public static string Obj2Json(object obj)
{
//把集合 转成 json 数组格式字符串
return jss.Serialize(obj);
}
}
}

2.写好等下我们要用的一般处理程序 handler.ashx 代码如下

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
namespace WebApplication1
{

public class Student
{

private string name;

public string Name
{
get { return name; }
set { name = value; }
}
private string pass;

public string Pass
{
get { return pass; }
set { pass = value; }
}

}
/// <summary>
/// handler 的摘要说明
/// </summary>
public class handler : IHttpHandler
{

public void ProcessRequest(HttpContext context)
{
List<Student> students = new List<Student>();
for (int i = 0; i < 5; i++)
{
Student stu = new Student() { Name="Name"+i,Pass="Pass"+i};
students.Add(stu);

}
string datas = DataHelper.Obj2Json(students);

AjaxMsgHelper.AjaxMsg("200","hello",datas);
}

public bool IsReusable
{
get
{
return false;
}
}
}
}

3.新建Comjs文件夹 在里面写一个msgbox.js文件

代码如下

/*{
imghref:图片文件夹所在路径,
waitImg:等待图片名,
bgImg:背景图片名
}
*/
function MsgBox(s) {
var secondConst = 2000;//系统默认显示时间
var secondWait = 2000;//显示时间
var timer;//计时器
var lf, tp;//左边距,顶边距
var paras = {}; //json参数
function readyMsgBox() {
if (s != null){
if (s.imghref != null) paras.imghref = s.imghref; else paras.imghref = "images/";
if (s.waitImg != null) paras.waitImg = s.waitImg; else paras.waitImg = "loader.gif";
if (s.bgImg != null) paras.bgImg = s.bgImg; else paras.bgImg = "qzonebg.gif";
}
else paras = { imghref: "./images/", waitImg: "loader.gif", bgImg: "qzonebg.gif" };
paras.waitImgTag = "<img src='" + paras.imghref + paras.waitImg + "' style='margin-right:10px;' align='middle'/> ";
preloadImg(new Array(paras.imghref + paras.bgImg, paras.imghref + paras.waitImg));
writeMsgBox();
window.onresize = function(){setPosition();}
}
this.showMsgWait = function (msg) {
this.showMsgAllT(paras.waitImgTag + msg, 0);
}
this.showMsgAllT = function (msg, type) {
clearTimer();
changeIco(type);
gelContainer().innerHTML = msg;
showBox();
}
this.hidBox = function () { hideBox(); };
this.showMsgText = function (msg) {
showMsgAllT(msg, 0);
}
this.showMsgInfo = function (msg) {
if (arguments.length > 1) paras.callBack = arguments[1];
showSysMsg(msg, 1);
}
this.showMsgInfoSide = function (eleId, msg, doHid) {//doHid 是否消失
if (arguments.length > 3) paras.callBack = arguments[1];
showSysMsgSideEle(eleId, msg, 1, doHid);
}
function analysisPara(args) {
if (args.length > 1) paras.callBack = args[1];
}
this.showMsgOk = function (msg) {
if (arguments.length > 1) paras.callBack = arguments[1];
showSysMsg(msg, 2);
}
this.showMsgOkSide = function (eleId, msg, doHid) {
if (arguments.length > 3) paras.callBack = arguments[1];
showSysMsgSideEle(eleId, msg, 2, doHid);
}
this.showMsgErr = function (msg) {
if (arguments.length > 1) paras.callBack = arguments[1];
showSysMsg(msg, 3);
}
this.showMsgErrSide = function (eleId,msg,doHid) {
if (arguments.length > 3) paras.callBack = arguments[1];
showSysMsgSideEle(eleId, msg, 3, doHid);
}
this.showSysMsgWTime = function (msg, type, second) {
if (arguments.length > 3) paras.callBack = arguments[3];
changeIco(type);
gelContainer().innerHTML = msg;
showBox();
secondWait = second;
if (second >= 0)
startTimer(emptyMsg);
}
function showSysMsg(msg, type) {
changeIco(type);
gelContainer().innerHTML = msg;
showBox();
secondWait = secondConst;
startTimer(emptyMsg);
}
//---显示在元素右边
function showSysMsgSideEle(eleId, msg, type, doHid) {
changeIco(type);
gelContainer().innerHTML = msg;
setPosSideEle(eleId);
if (doHid) {
secondWait = secondConst;
startTimer(emptyMsg);
} else clearTimer();
}
function setPosSideEle(eleId) {
var wid = document.getElementById(eleId).offsetWidth;
var hig = document.getElementById(eleId).offsetHeight;
var pos = getPos(eleId);
gelBox().style.left = (wid+2 + pos.left) + "px";
gelBox().style.top = (pos.top - (hig/2)) + "px";
gelBox().style.display = "block";
}
//--------------
this.showReqErr=function(){this.showMsgErr("请求错误 ToT!");}
this.showReqOk=function(){this.showMsgOk("操作成功 ^o^!");}
this.showReqVF = function () { this.showSysMsgWTime("会话过期,3秒后自动返回登录界面 -o-!",1,3000); }
this.showWait = function () { this.showMsgWait("请稍后 l _ l ..."); }
//-------------
function startTimer(functionName) {
clearTimer();
timer=window.setTimeout(functionName, secondWait);
}
function clearTimer() {
if (timer != null && timer != undefined) { clearTimeout(timer); }
}
function emptyMsg() {
gelContainer().innerHTML = "";
hideBox();
if (paras.callBack != null) {paras.callBack(); paras.callBack = null; }
}
function writeMsgBox() {
var msgBox = document.createElement("table");
var msgTbody = document.createElement("tbody");
var msgTr = document.createElement("tr");
var msgBoxL = document.createElement("td");
var msgBoxC = document.createElement("td");
var msgBoxR = document.createElement("td");
document.body.appendChild(msgBox);
msgBox.appendChild(msgTbody);
msgTbody.appendChild(msgTr);
msgTr.appendChild(msgBoxL);
msgTr.appendChild(msgBoxC);
msgTr.appendChild(msgBoxR);
msgBox.setAttribute("id", "msgBox");
msgBox.setAttribute("cellpadding", "0");
msgBox.setAttribute("cellspacing", "0");
msgBox.style.cssText = "height:52px;width:auto;position:absolute;z-index:999999;display:none; background:url(" + paras.imghref + paras.bgImg+") 0px -161px;";
msgBoxL.setAttribute("id", "msgBoxL");
msgBoxL.style.cssText = "width:50px;background:url(" + paras.imghref + paras.bgImg+") -7px -108px no-repeat;";
msgBoxC.setAttribute("id", "msgBoxC");
msgBoxC.style.cssText = "width:auto;line-height:51px;color:#666666;font-weight:bold;font-size:14px;padding-right:10px;";
msgBoxR.setAttribute("id", "msgBoxR");
msgBoxR.style.cssText = "width:5px;background:url(" + paras.imghref + paras.bgImg+") 0px 0px no-repeat;";
}
function changeIco(ty) {
if (ty == 0)//none
document.getElementById("msgBoxL").style.width = "10px";
else document.getElementById("msgBoxL").style.width = "50px";
if (ty == 1)//info
document.getElementById("msgBoxL").style.backgroundPosition = "-7px -54px";
else if (ty == 2)//ok
document.getElementById("msgBoxL").style.backgroundPosition = "-7px 0px";
else if (ty == 3)//err
document.getElementById("msgBoxL").style.backgroundPosition = "-7px -108px";
}
function gelBox() {
return document.getElementById("msgBox");
}
function gelContainer() {
return document.getElementById("msgBoxC");
}
function hideBox() {
gelBox().style.display = "none";
}
function showBox() {
setPosition();
gelBox().style.display = "block";
}
function setPosition() {
lf = document.body.clientWidth / 2 - (gelBox().innerHTML.replace(/<[^>].*?>/g, "").length) * 10;
tp = window.screen.height / 2 - 200 + document.documentElement.scrollTop;
gelBox().style.left = lf + "px";
gelBox().style.top = tp + "px";
}
function preloadImg() {
var Arrimg = new Array();
if (typeof (arguments[0]) == "string") Arrimg[0] = arguments[0];
if (typeof (arguments[0]) == "object") {
for (var i = 0; i < arguments[0].length; i++) {
Arrimg[i] = arguments[0][i];
}
}
var img = new Array()
for (var i = 0; i < Arrimg.length; i++) {
img[i] = new Image();
img[i].src = Arrimg[i];
}
}
function getPos(eid) {var target = document.getElementById(eid);var left = 0, top = 0;
do {left += target.offsetLeft || 0;top += target.offsetTop || 0;target = target.offsetParent;} while (target);
return {left: left,top: top}
}
readyMsgBox();
}

4.在要使用ajax的页面写入如下代码

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<style type="text/css">
#div1
{
border:1px solid red;
display:block;
}

</style>
<script src="Comjs/jquery-1.8.0.min.js" type="text/javascript"></script>
<script src="Comjs/jquery-1.8.0.js" type="text/javascript"></script>
<script src="Comjs/msgBox.js"></script>
<script src="Comjs/PageBar.js"></script>

<script type="text/javascript">
var msgbpx = null;

$(function () {

msgbpx = new MsgBox();
// msgbpx.showMsgErr("好奇怪没放映");
$.ajax({
url:"Comjs/handler.ashx",
type: "get",

dataType: "json",
data: 'dj=' + Math.random(),
success: function (json) {

if (json.data!=null) {
msgbpx.showMsgErr("wait");
}
var inhtml = "";
inhtml += "<div><table><tr><td>用户名</td><td>密码</td></tr>";
for (i = 0; i < json.data.length; i++) {
var stu = json.data[i];
// msgbpx.showMsgInfo(tu.Pass);
inhtml += "<tr><td>" + stu.Name+ "</td>";

inhtml += "<td>" + stu.Pass+ "</td></tr>";
//alert("为什么");
}
inhtml += "</div>";
$('#div1').html(inhtml);

},
error: function (xhr, status, errorThrown) {
alert("errorThrown=" + errorThrown);
}
})

});
</script>
</head>
<body>
<form id="form1" runat="server">
<div id="div1" style="height:500px; width:500px;">

</div>
</form>
</body>
</html>

jqueryAjax的使用的更多相关文章

  1. ECharts SSH+JQueryAjax+Json+JSP将数据库中数据填充到ECharts中

    本文引用自:http://blog.csdn.net/ArcticFoxHan/article/details/38071641   1.导入包,搭建SSH框架 导入Jquery的JS包,<sc ...

  2. php用jquery-ajax上传多张图片限制图片大小

    php用jquery-ajax上传多张图片限制图片大小 /** * 上传图片,默认大小限制为3M * @param String $fileInputName * @param number $siz ...

  3. ASP.NET使用Jquery-Ajax向ashx传递参数中文出现乱码

    今天遇到个问题,IE11下Jquery-Ajax向ashx传递参数中文出现乱码,但在谷歌.火狐.360等浏览器中没有乱码的问题,百度了好久最后发现使用escape()对参数值进行处理就可以了: 参考代 ...

  4. Hibernate+struts+JqueryAjax+jSON实现无刷新三级联动

    看网上JqueryAjax三级联动的例子讲不是很全,代码也给的不是很全,给初学者带来一定的难度.小弟自己写了一个,可能有些地方不是很好,希望大家能够提出建议. 用的是Hibernate+struts2 ...

  5. AJAX校验用户名是否存在,焦点离开用户名、点击 【 检 查用户名 】的校验。分别用 XMLHttp 和 JQueryAJAX实现。

     XMLHttp方法: $("#name").blur(function () { var xmlhttp = new ActiveXObject("Microsoft. ...

  6. JQuery--Ajax 异步操作 动态添加节点 (新人试水,求支持)

    异步操作动态添加节点,导致在代码中给添加的节点全局绑定事件或者获取元素无效,上代码: $(function () { var IP = '...'; // 页面中的默认编号起始值 和 公用IP前缀 s ...

  7. jquery-ajax实现文件上传异常处理web.multipart.MultipartException

    异常如下: org.springframework.web.multipart.MultipartException: The current request is not a multipart r ...

  8. 【jQuery】(7)---jQueryAjax同步异步区别

    jQueryAjax同步异步 今天在项目开发过程中,要实现这么一个功能 <!-- 当我点击就业的时候,触发onclick时间,check()方法里通过ajax请求返回数据, 如果该用户已经毕业可 ...

  9. 最好用的jQuery-Ajax缓存插件

    AJAX-Cache    最好用的jQuery-Ajax缓存插件 介绍 AJAX-Cache是一款jQuery插件,基于localStorage/sessionStorage实现异步请求缓存功能,并 ...

  10. 推荐一款jQueryajax插件(Ajaxify jQuery )

    推荐一款jQueryajax插件(Ajaxify jQuery )       此插件相当强悍,但最后一个版本是在2008年,作者很久没更新了,我在寻找了好多关羽ajax的工具,没有发现比这个更灵活的 ...

随机推荐

  1. msq_table's methods2

    -- 删除数据 自增长id被占用 -- 清楚所有数据并重置id 1 truncate table name; -- 主键(唯一) id int primary key; -- 主键内容不能重复,不能为 ...

  2. python作用域与LEGB规则

    作用域 什么是命名空间 比如有一个学校,有10个班级,在7班和8班中都有一个叫“小王”的同学,如果在学校的广播中呼叫“小王”时,7班和8班中的这2个人就纳闷了,你是喊谁呢!!!如果是“7班的小王”的话 ...

  3. Xshell图形界面启动

    https://blog.csdn.net/qq_27843481/article/details/50539797 增加内存:https://jingyan.baidu.com/article/4d ...

  4. curl 请求https内容,返回空

    $ch = curl_init(); curl_setopt($ch, CURLOPT_URL,$api); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);/ ...

  5. 前端-CSS-7-标准文档流&行内元素和块级元素转换

    1.什么是标准文档流 <!--  什么是标准文档流 宏观的将,我们的web页面和ps等设计软件有本质的区别 web 网页的制作 是个“流” 从上而下 ,像 “织毛衣” 而设计软件 ,想往哪里画东 ...

  6. 3 Python 函数介绍

    1.函数的基本概念 定义: 函数是指将一组语句的集合通过一个名字(函数名)封装起来,要想执行这个函数,只需调用其函数名即可 特性: 减少重复代码 使程序变的可扩展 使程序变得易维护 语法定义 def ...

  7. Window python下载安装

    Window python下载安装 http://www.runoob.com/python/python-install.html https://pan.baidu.com/s/1MoR9nWUY ...

  8. 根据img的url 判断img的图片大小

    // 图片地址 后面加时间戳是为了避免缓存 var img_url = 'http://www.qttc.net/static/upload/2013/13643608813441.jpg?'+Dat ...

  9. Linux 的伪终端的基本原理 及其在远程登录(SSH,telnet等)中的应用

    本文介绍了linux中伪终端的创建,介绍了终端的回显.行缓存.控制字符等特性,并在此基础上解释和模拟了telnet.SSH开启远程会话的过程. 一.轻量级远程登录 之前制作的一块嵌入式板子,安装了嵌入 ...

  10. 获取tomcat源码

    1.安装环境 工欲善其事必先利其器,在开始工作之前,先准备好环境和工具:jdk.eclipse.并配置eclipse的svn插件.ant,具体怎么配置就不说了,网上都有. 2.下载源码 打开eclip ...