如何在ajax请求中设置特殊的RequestHeader
1个回答
展开全部
function get(url, fn){
//建立ajax对象
var xhr;
if(window.XMLHttpRequest){
xhr = new XMLHttpRequest();
}else{
xhr = new ActiveXObject("Msxml2.XMLHTTP");
}
//建立连接
xhr.open("GET", url, true);
//发送请求
xhr.send();
//接收响应
xhr.onreadystatechange = function(){
if( xhr.readyState == 4 && xhr.status == 200 ){
if( fn ){
fn(xhr.responseText);//回调函数
}
}
}
}
function post(url, arg, fn){
//建立ajax对象
var xhr;
if(window.XMLHttpRequest){
xhr = new XMLHttpRequest();
}else{
xhr = new ActiveXObject("Msxml2.XMLHTTP");
}
//建立连接
xhr.open("POST", url, true);
//发送请求
xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
xhr.send(arg);
//接收响应
xhr.onreadystatechange = function(){
if( xhr.readyState == 4 && xhr.status == 200 ){
if( fn ){
fn(xhr.responseText);//回调函数
}
}
}
}
//建立ajax对象
var xhr;
if(window.XMLHttpRequest){
xhr = new XMLHttpRequest();
}else{
xhr = new ActiveXObject("Msxml2.XMLHTTP");
}
//建立连接
xhr.open("GET", url, true);
//发送请求
xhr.send();
//接收响应
xhr.onreadystatechange = function(){
if( xhr.readyState == 4 && xhr.status == 200 ){
if( fn ){
fn(xhr.responseText);//回调函数
}
}
}
}
function post(url, arg, fn){
//建立ajax对象
var xhr;
if(window.XMLHttpRequest){
xhr = new XMLHttpRequest();
}else{
xhr = new ActiveXObject("Msxml2.XMLHTTP");
}
//建立连接
xhr.open("POST", url, true);
//发送请求
xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
xhr.send(arg);
//接收响应
xhr.onreadystatechange = function(){
if( xhr.readyState == 4 && xhr.status == 200 ){
if( fn ){
fn(xhr.responseText);//回调函数
}
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询