JS 发送POST

var postData = {
    "name1": "value1",
    "name2": "value2"};
 
postData = (function(obj){ // 转成post需要的字符串.
    var str = "";
 
    for(var prop in obj){
        str += prop + "=" + obj[prop] + "&"
    }
    return str;
})(postData);
 
var xhr = new XMLHttpRequest();
 
xhr.open("POST", "../module", true);
xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xhr.onreadystatechange = function(){
    var XMLHttpReq = xhr;
    if (XMLHttpReq.readyState == 4) {
        if (XMLHttpReq.status == 200) {
            var text = XMLHttpReq.responseText;
 
            console.log(text);
        }
    }
};
xhr.send(postData);

 

posted @ 2015-10-29 14:17  328201736  阅读(395)  评论(0编辑  收藏  举报