封装一个简单的Ajax函数
/**
* Ajax操作函数
*
* @param url -- 服务器端页面地址
* @param param -- 参数,类似 'user=123&id=100'
* @param method -- 请求服务器端的方法,Get和Post两种,默认是GET
* @param response -- 是否获取服务器端返回的结果,默认是true
*/
function ajax( url, param, method, response ){
//set default value
param = typeof(param)=='undefined' ? '' : param;
method = typeof(method)=='undefined' ? 'GET' : method;
response = typeof(response)=='undefined' ? true : response;
//get ajax object
var ajax = null;
try {
ajax = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
ajax = new ActiveXObject("Microsoft.XMLHTTP");
} catch (E) {
ajax = null;
}
}
if (!ajax && typeof XMLHttpRequest!='undefined'){
ajax = new XMLHttpRequest();
}
if (!ajax){
alert("Get ajax object failed");
return false;
}
//send http request
var res = '';
if (method != 'GET'){
method = 'POST';
}
if (method == 'GET'){
ajax.open('GET', url + param, true);
ajax.onreadystatechange = function(){
if (ajax.readyState==4 && ajax.status==200){
res = ajax.responseText;
}
}
ajax.send(null);
}
if (method == "POST"){
ajax.open("POST", url, true);
ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
ajax.send(param);
ajax.onreadystatechange = function() {
if (ajax.readyState==4 && ajax.status==200) {
res = ajax.responseText;
}
}
}
if (response){
return res;
}
return null;
}
相关文档:
解决Ajax传送中文会导致乱码的问题
//如果传送参数是直接赋予的,就会产生乱码!
http_request.open("POST",url,true);
http_request.setRequestHeader('Content-Type',
'application/x-www-form-urlencoded');
http_request.send("action="+strName+"&val="+val);
......
今天的话题是如何改进自己网站的界面或提高网站的视觉体验,从而让用户记忆犹新。
我们有三种主要的方法(从难到易):自己动手写脚本;使用类似于jQuery和mooTools的JavaScript框架(可以让编写代码变得更容易些);使用能工作于现有的JavaScript框架下的提前预置好的脚本或那种从头开始开发的创建者。这篇文章适合那 ......
===============下骗Ajax
================
十一、第十一课 ===》 使用XHR对象发送和接受数据
a.继续上面步骤
2)注册回调函数,只写函数名称(如果加了括号,那么就把函数返回值注册给回调函数)
xmlHttp.onreadystatechange = callback;
3)设置连接信息
//第一参数是Http请求方式,一般选择get、pos ......
这几天工作,用的是AJAX框架,结果导出Excel表格的时候无法导出。
解决办法:1、在.aspx页面首行中<%@ Page Language="C#" AutoEventWireup="true" CodeFile="JinHuoDan.aspx.cs" Inherits="out_store_JinHuoDan" %> 添加EnableEventValidation="false" ;
2、在UPdatePanel中添加</ContentTemplate>
......
Ajax框架 jQuery
jQuery是一款免费且开放源代码的JavaScript代码库,由John Resig创建。授权协议为GPL和MIT许可证双协议。示例代码: $("p.neat").addClass("ohmy").show("slow"); 特点轻量级经GZip压缩后传输的代码文件仅14KB,未经压缩传送的代码文件仅26KB。 jQuery 1.26版时: 档案档案行数档案Size 备注 jquery-1.2 ......