js发送ajax请求
2014-10-09 14:16
141 查看
function
initRequest()
{
var request = false;
if(window.XMLHttpRequest) {
//FireFox
request = new XMLHttpRequest();
if (request.overrideMimeType) {
request.overrideMimeType('text/xml');
}
}
else if
(window.ActiveXObject) {
//IE
try {
request = new
ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
request = new
ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
if (!request)
{
window.alert("Create request error!");
return false;
}
return
request;
}
var http_request;
function
send(sendUrl,sendData)
{
http_request =
initRequest();
sendUrl +=
"&random="+Math.random();
//在URL后添加这个,可以保证不被缓存
http_request.onreadystatechange =
ajax_call_back;//指定请求返回时的回调函数
//get
http_request.open("GET", sendUrl,true);
http_request.send(sendData);
//post
//http_request.open("POST", sendUrl, true);
//http_request.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
//http_request.send(sendData);
}
function
ajax_call_back()
{
//readyState共有5中状态,0未初始化,1已初始化,2发送请求,3开始接收结果,4接收结果完毕。
//status服务器响应状态码
if
(http_request.readyState == 4) {
if (http_request.status == 200) {
var str =
http_request.responseText;
alert("返回数据为:"+str);
}
else if(http_request.status == 404){
alert("请求资源不存在!");
}
else {
alert("Ajax请求失败,错误状态为:"+http_request.status);
}
}
}
initRequest()
{
var request = false;
if(window.XMLHttpRequest) {
//FireFox
request = new XMLHttpRequest();
if (request.overrideMimeType) {
request.overrideMimeType('text/xml');
}
}
else if
(window.ActiveXObject) {
//IE
try {
request = new
ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
request = new
ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
if (!request)
{
window.alert("Create request error!");
return false;
}
return
request;
}
var http_request;
function
send(sendUrl,sendData)
{
http_request =
initRequest();
sendUrl +=
"&random="+Math.random();
//在URL后添加这个,可以保证不被缓存
http_request.onreadystatechange =
ajax_call_back;//指定请求返回时的回调函数
//get
http_request.open("GET", sendUrl,true);
http_request.send(sendData);
//post
//http_request.open("POST", sendUrl, true);
//http_request.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
//http_request.send(sendData);
}
function
ajax_call_back()
{
//readyState共有5中状态,0未初始化,1已初始化,2发送请求,3开始接收结果,4接收结果完毕。
//status服务器响应状态码
if
(http_request.readyState == 4) {
if (http_request.status == 200) {
var str =
http_request.responseText;
alert("返回数据为:"+str);
}
else if(http_request.status == 404){
alert("请求资源不存在!");
}
else {
alert("Ajax请求失败,错误状态为:"+http_request.status);
}
}
}
XMLHttpRequest 对象 | |
属性 | |
readyState | 请求状态,开始请求时值为0直到请求完成这个值增长到4 |
responseText | 目前为止接收到的响应体,readyState<3此属性为空字符串,=3为当前响应体,=4则为完整响应体 |
responseXML | 服务器端相应,解析为xml并作为Document对象返回 |
status | 服务器端返回的状态码,=200成功,=404表示“Not Found” |
statusText | 用名称表示的服务器端返回状态,对于“OK”为200,“Not Found”为400 |
方法 | |
setRequestHeader() | 向一个打开但是未发生的请求设置头信息 |
open() | 初始化请求参数但是不发送 |
send() | 发送Http请求 |
abort() | 取消当前相应 |
getAllResponseHeaders() | 把http相应头作为未解析的字符串返回 |
getResponseHeader() | 返回http相应头的值 |
事件句柄 | |
onreadystatechange | 每次readyState改变时调用该事件句柄,但是当readyState=3有可能调用多次 |
相关文章推荐
- js 发送ajax请求
- js 发送ajax请求(XMLHttpRequest)
- 在发送ajax请求时加时间戳或者随机数去除js缓存
- 使用原生JS发送ajax异步请求
- js使用ajax发送请求,后台用RESTful服务
- (Ext / Js) ajax 跨域请求发送两次解决方案
- 在发送ajax请求时加时间戳或者随机数去除js缓存
- js中使用队列发送ajax请求
- JSP中在提交表单之前,发送ajax请求进行js的验证
- js通过ajax发送请求赋值全局变量
- 原生JS发送ajax请求
- JS将form表单转换为json数据,使用ajax发送请求案例
- 原生js实现ajax 发送post请求
- 利用Ajax向Servlet发送请求下载统计后的Excel文件,利用jquery.blockUI.js给予用户等待提示
- js与jQuery终止正在发送的ajax请求的方法
- 在发送ajax请求时加时间戳或者随机数去除js缓存
- Js/Ajax中发送HttpPost请求调用WebService
- MVC设计模式 (1)、html页面事件触发ajax()方法调用ajax.js发送请求至请求目标文件
- Extjs ajax同步请求时post方式参数发送方式
- Ajax的GET与POST方式向服务器发送请求