javascript怎么进行http请求数据demo
1个回答
2016-07-08
展开全部
javascript中向服务器发送http请求借助的是XMLHttpRequest对象,其他一些库如JQuery对http的请求应该是在这个上面的封装,创建XMLHTPRequest对象用下面的语句:
function createXMLHttpRequest() {
var xmlHttp;
if (window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
if (xmlHttp.overrideMimeType)
xmlHttp.overrideMimeType('text/xml');
} else if (window.ActiveXObject) {try {xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {try {xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}}}return xmlHttp;}
发送get请求,并异步处理
xmlHttp = createXMLHttpRequest();
var url = "getfiledetail.jsp?fileid="+id;
xmlHttp.open("GET", url, true);// 异步处理返回
xmlHttp.onreadystatechange = callback;
xmlHttp.setRequestHeader("Content-Type",
"application/x-www-form-urlencoded;");
xmlHttp.send();
发送post请求
var url = "getNginxStatus";
xmlHttp.open("POST", url, true);
xmlHttp.onreadystatechange = getStatusBack;
xmlHttp.setRequestHeader("Content-Type",
function createXMLHttpRequest() {
var xmlHttp;
if (window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
if (xmlHttp.overrideMimeType)
xmlHttp.overrideMimeType('text/xml');
} else if (window.ActiveXObject) {try {xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {try {xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}}}return xmlHttp;}
发送get请求,并异步处理
xmlHttp = createXMLHttpRequest();
var url = "getfiledetail.jsp?fileid="+id;
xmlHttp.open("GET", url, true);// 异步处理返回
xmlHttp.onreadystatechange = callback;
xmlHttp.setRequestHeader("Content-Type",
"application/x-www-form-urlencoded;");
xmlHttp.send();
发送post请求
var url = "getNginxStatus";
xmlHttp.open("POST", url, true);
xmlHttp.onreadystatechange = getStatusBack;
xmlHttp.setRequestHeader("Content-Type",
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询