原生javascript实现ajax,post参数
1 var json = {
2 userid: userid,
3 cid: cid,
4 openid: openid,
5 type: 1
6 };
7 // 原生ajax
8 json = (function(obj){ // 转成post需要的字符串.
9 var str = "";
10 for(var prop in obj){
11 str += prop + "=" + obj[prop] + "&"
12 }
13 return str;
14 })(json);
15 var xhr = new XMLHttpRequest();
16 xhr.open("POST", "/sharecount", true);
17 xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");
18 xhr.onreadystatechange = function(){
19 var XMLHttpReq = xhr;
20 if (XMLHttpReq.readyState == 4) {
21 if (XMLHttpReq.status == 200) {
22 //不做处理
23 }
24 }
25 }
26 xhr.send(json);
坚持下去就能成功