原生js的ajax怎么传json数据(不用jQuery)

原生js的ajax怎么传json数据(不用jQuery)
.

自己用js转为键值对字符串,如果你要专递原始json格式的字符串,用JSON.parse转为字符串后传递,IE7-不只支持json对象,需要倒入json2.js

   var xhr = window.XMLHttpRequest ? new XMLHttpRequest() : window.ActiveXObject ? new ActiveXObject("microsoft.xmlhttp") : false;
    if (xhr) {
        var o = { a: 1, b: 2, c: 3 }
        var s = '';
        //键值对
        for (k in o) s += '&' + k + encodeURIComponent(o[k]);
        s = s.substring(1);
        //发送JSON格式字符串
        //s = "data=" + encodeURIComponent(JSON.stringify(o));
        xhr.open("post", "url地址");
        xhr.setRequestHeader("content-type", "application/x-www-form-urlencoded");
        xhr.send(s)
    }
    else alert('不支持ajax')