ajax的post请求与编码

window.onload = function(){
  document.getElementById('username').onblur = function(){
    var name = document.getElementById('username').value;
    name = encodeURIComponent(name); // 编码
    var request = new XmlHttpRequest();
    request.open('post', 'demo.php');
    request.onreadystatechange = function(){
      if (request.readystate == 4 && request.status == 200) {
        var str = request.responseText();
        alert(str);
      }
    }
    request.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
    request.send('name='+name);
  }
}

posted @ 2018-05-12 20:12  wangzhenxiang  阅读(1059)  评论(0编辑  收藏  举报