C# XMLHttpRequest对象—Ajax实例
Get:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<script type="text/javascript">
window.onload = function () {
document.getElementById("btnGet").onclick = function () {
//第一步,创建XMLHttpRequest对象
var xhr = null;
if (typeof (XMLHttpRequest) != undefined) {
xhr = new XMLHttpRequest();
}
else {
xhr = new ActiveXObject("Microsoft.XMLHttp");
}
//第二部,设置异步回调函数。
xhr.onreadystatechange = function () {
//xhr对象状态,0=已创建XMLHttpRequest对象,1=open,2=send,3=onready等待响应,4=成功。
if (xhr.readyState == 4) {
//status 响应状态
if (xhr.status >= 200 && xhr.status < 300 || xhr.status == 304) {
document.getElementById("div1").innerHTML = xhr.responseText; //xhr.responseText 响应体
}
}
}
//第三步,打开对象,设置请求方式和访问路径
xhr.open("Get", "GetTime.ashx?id=17&name=" + window.encodeURIComponent("张三"), true);
//第四步,send()
xhr.send(null);
};
};
</script>
</head>
<body>
<div id="div1"></div>
<input type="button" value="无刷新异步获取" id="btnGet" />
</body>
</html>
Post:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<script type="text/javascript">
window.onload = function () {
document.getElementById("btn").onclick = function () {
var xhr = null;
if (typeof (XMLHttpRequest) != undefined) {
xhr = new XMLHttpRequest();
}
else {
xhr = new ActiveXObject("Microsoft.XMLHttp");
}
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && xhr.status == 200) {
document.getElementById("div1").innerHTML = xhr.responseText + "<hr />";
//获取响应报文头信息
var date = xhr.getResponseHeader("Date");
document.getElementById("div2").innerHTML = date + "<hr />";
//获取所有响应报文头信息
var all = xhr.getAllResponseHeaders();
document.getElementById("div3").innerHTML = all + "<hr />";
}
}
//通过Post方式请求
xhr.open("Post", "GetTime.ashx", true);
//需要添加请求报文头,Content-Type.
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
//通过键值对的方式传值。
var name = document.getElementById("name").value;
xhr.send("id=18&name=" + name);
};
};
</script>
</head>
<body>
<div id="div1"></div>
<div id="div2"></div>
<div id="div3"></div>
<input type="text" id="name" value="李四" />
<input type="button" value="提交" id="btn" />
</body>
</html>
GetTime.ashx:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Web; namespace MyPerson.UI.Ajax
{
/// <summary>
/// GetTime 的摘要说明
/// </summary>
public class GetTime : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
//Thread.Sleep(1000);
string id = context.Request["id"];
string name = context.Request["name"];
context.Response.Write(DateTime.Now.ToString() + "<br/>编号:" + id + "<br/>姓名:" + name);
} public bool IsReusable
{
get
{
return false;
}
}
}
}
JQuery版:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<script src="../Scripts/jquery-1.7.1.js"></script>
<script type="text/javascript">
$(function () {
$("#btnGet").click(function () {
$.ajax({
type: "GET",
url: "GetTime.ashx",
data: {id: 17, name: "张三"},
dataType: "text",
success: function(data) {
$('#div1').html(data);
}
});
});
});
</script>
</head>
<body>
<div id="div1"></div>
<input type="button" value="无刷新异步获取" id="btnGet" />
</body>
</html>
C# XMLHttpRequest对象—Ajax实例的更多相关文章
- XmlHttpRequest对象 ajax核心之一
XMLHttpRequest 对象 XML XSLT XML 解析器 XMLHttpRequest 对象用于在后台与服务器交换数据. 什么是 XMLHttpRequest 对象? XMLHttpReq ...
- Ajax 学习之创建XMLHttpRequest对象------Ajax的核心
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...
- 使用XMLHttpRequest对象完成原生的AJAX请求
1.大家眼中的Ajax 说到Ajax,只要有过前端开发经验的童鞋一定都不陌生,大都知道它就是一种与后端之间的通信技术,通过这个神奇的家伙,我们不用像传统表单那样填完信息一点提交就呼啦呼啦跳转了.Aja ...
- Ajax中的XMLHttpRequest对象详解
XMLHttpRequest对象是Ajax技术的核心.在Internet Explorer 5中,XMLHttpRequest对象以ActiveX对象引入,被称之为XMLHTTP,它是一种支持异步请求 ...
- Ajax学习(三)——XMLHttpRequest对象的五步使使用方法
Ajax的核心技术是XMLHttpRequest对象,它能够在不向server提交整个页面的情况下.实现局部更新网页.通过这个对象,Ajax能够像桌面应用程序那样仅仅与server进行数据层的 ...
- AJAX 核心 —— XMLHTTPRequest 对象回顾
一.AJAX概述 不使用 AJAX 的网页,如果要更新内容,需要重载整个页面. AJAX ( Asynchronous Javascript And XML ,异步 Javascript 和 XML) ...
- Ajax中的XMLHttpRequest对象详解(转)
XMLHttpRequest对象是Ajax技术的核心.在Internet Explorer 5中,XMLHttpRequest对象以ActiveX对象引入,被称之为XMLHTTP,它是一种支持异步请求 ...
- AJAX——XMLHttpRequest对象的使用
AJAX是web2.0即动态网页的基础,而XMLHttpRequest对象又是AJAX的核心.XMLHttpRequest对象负责将用户信息以异步通信地发送到服务器端,并接收服务器响应信息和数据 一. ...
- Ajax技术---核心XMLHttpRequest对象
Ajax 的全称是Asynchronous JavaScript and XML,其中,Asynchronous 是异步的意思,它有别于传统web开发中采用的同步的方式. (一)ajax技术的意义 我 ...
随机推荐
- Fantasy of a Summation n个数,k层重复遍历相加。求它的和%mod的值;推导公式+快速幂
/** 题目:Fantasy of a Summation 链接:https://vjudge.net/contest/154246#problem/L 题意:n个数,k层重复遍历相加.求它的和%mo ...
- Unity3D游戏开发之游戏读/存档功能在Unity3D中的实现
喜欢我的博客请记住我的名字:秦元培,我的博客地址是:http://qinyuanpei.com 转载请注明出处,本文作者:秦元培, 本文出处:http://blog.csdn.net/qinyuanp ...
- golang解析xml
解析xml标签或者html标签,都是xml文档格式.要是返回的html标签,可以用第三方依赖库goquery来解析. 下面说下,解析xml标签的格式.直接上代码,代码如下: package main ...
- jQuery 尺寸 方法
jQuery 提供多个处理尺寸的重要方法: width() height() innerWidth() innerHeight() outerWidth() outerHeight()
- CFindReplaceDialog学习
The CFindReplaceDialog class allows you to implement standard string Find/Replace dialog boxes in yo ...
- odata配置控制器方法路由1
查看edmx:http://localhost:12769/odata/$metadata 1.配置 ODataConventionModelBuilder builder = new ODataCo ...
- 【BZOJ5056】OI游戏 最短路+有向图生成树计数
[BZOJ5056]OI游戏 Description 小Van的CP最喜欢玩与OI有关的游戏啦~小Van为了讨好她,于是冥思苦想,终于创造了一个新游戏. 下面是小Van的OI游戏规则: 给定一个无向连 ...
- 男神的约会(状压dp)
有一天男神约了学姐姐去看电影,电影院有一个活动,给你一个10*10的矩阵,每一个格子上都有一个0-9的整数,表示一共十种优惠券中的一种. 观众从左上角的格子开始走,走到右下角.每走到一个有着a号优惠券 ...
- DRDS和RDS主要用来存储用户交易信息,MongoDB主要用来存储商品维度信息
数据集成Data Integration-数加-大数据-阿里云 https://www.aliyun.com/product/cdp 数据集成支持的数据源 数据源类型 数据源 来源数据源被读取 目标数 ...
- pigeonhole principle 哈希表的重复问题(冲突)是不可避免的
https://en.wikipedia.org/wiki/Pigeonhole_principle Sock-picking Assume a drawer contains a mixture o ...