原生: try { var data=""; var xmlhttp; if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp = new XMLHttpRequest(); } else {// code for IE6, IE5 xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange = function () { if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { var word = xmlhttp.responseText;//服务器端返回的内容 } } xmlhttp.open("POST", "./XXXX.ashx", true); xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencode"); xmlhttp.send(data); //或者get方式 //xmlhttp.open("GET", "./XXXX.ashx", true); //xmlhttp.send(); } catch (err) { }
json只是返回值的一种格式,具体格式自行百度,可以到bejson去看下你写的json对不对 返回json字符串之后, var word = xmlhttp.responseText;//服务器端返回的内容,然后var json=JSON.parse(word)或者用其他方法把字符串转成对象就行了,之后就可以用json.a直接取其中的a项内容
源码:<%@ 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>
<script src="js/jquery-2.1.1.min.js"></script>//记得引用啊
<script type="text/javascript">
$(function () {
$.ajax({
type: "Post",
url: "WebForm1.aspx",
data: "token=ajax",
dataType: "json",
success: function (data) {
alert(data);
},
error: function (d, c, e) {
alert(e);
}
});
}) </script>
</head>
<body>
<form id="form1" runat="server">
<div id="dataShow">
</div>
</form>
</body>
</html>后台代码: protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
if ((Request["token"] ?? "") == "ajax")
{
// 如果要是返回的响应为xml,则必须这样设置
Response.ContentType = "application/json";
// 如果要是返回的响应为xml,返回的字符串必须是可以被解析的xml文档格式。
Response.Write("[123]");
// 下面这些内从可以放在一个方法里,然后通过“token”标记去判断执行哪个方法。
//Response.Write("我是直接请求aspx页面返回的文字!");
Response.End();
}
}
}
当然也可以用javascript写原生态的ajax,不过需要考虑浏览器的兼容性 比如这样<script language="javascript" type="text/javascript">
var request = false;
try {
request = new XMLHttpRequest();
} catch (trymicrosoft) {
try {
request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (othermicrosoft) {
try {
request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (failed) {
request = false;
}
}
}if (!request)
alert("Error initializing XMLHttpRequest!");
</script>
这是我之前写的。
先知道 ajax和 jquery到底是什么。
在了解 怎么通过jquery的 ajax,去后台异步取数据。
try {
var data="";
var xmlhttp;
if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
}
else {// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function () { if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
var word = xmlhttp.responseText;//服务器端返回的内容 }
} xmlhttp.open("POST", "./XXXX.ashx", true);
xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencode");
xmlhttp.send(data);
//或者get方式
//xmlhttp.open("GET", "./XXXX.ashx", true);
//xmlhttp.send();
}
catch (err) {
}
返回json字符串之后, var word = xmlhttp.responseText;//服务器端返回的内容,然后var json=JSON.parse(word)或者用其他方法把字符串转成对象就行了,之后就可以用json.a直接取其中的a项内容