<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="js/jquery.js"></script>
</head>
<body>
<button οnclick="xmlhttp()">xmlhttp</button>
</body>
<script>
function getHttpObj() {
var httpobj = null;
try {
httpobj = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e) {
try {
httpobj = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e1) {
httpobj = new XMLHttpRequest();
}
}
return httpobj;
}
function xmlhttp() {
// var xhr = new XMLHttpRequest();
var xhr = getHttpObj();
xhr.open("post", "http://www.abcd.com/*****", true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;");//缺少这句,后台无法获取参数
xhr.onreadystatechange = function() {
console.log(xhr.responseText);
if (xhr.readyState == 4 && xhr.status == 200) {
console.log(xhr.responseText);
}
};
var content = "appid=11111&sign=222222222";
xhr.send(content);
}
</script>
</html>
使用js编写跨域http的post请求
注意,服务器端须设置设置response'Access-Control-Allow-Origin',
有些朋友可能会用ajax的jsonp方法,但是jsonp只能提供get请求,对于限制post请求的接口则无法适用。
关于jsonp的实现原理,参照http://www.jb51.net/article/75669.htm