如何使用ajax.response获取telementbyid

How to getelementbyid using ajax.response

本文关键字:获取 telementbyid response ajax 何使用      更新时间:2023-09-26

我使用php发送回div名称到ajax调用使用post我想使用ajax得到的js变量然后用它来创建一个div:

idisis=ajax.responseText;
document.getElementById(idisis).innerHTML= some text; 
// ('idisis') || (''+idisis+'')|| ("'+idisis+'") 

但它不起作用;

这是我制作的一个原始XMLHttpRequest post函数,以及其他一些好东西。它是向后兼容的。

function phpEncode(obj){
  var r = [];
  if(obj instanceof Array){
    for(var i=0,l=obj.length; i<l; i++){
      r.push(phpEncode(obj[i]));
    }
    return '%5B'+r.join(',')+'%5D';
  }
  else if(typeof obj === 'object' && obj){
    for(var i in obj){
      if(obj.hasOwnProperty(i)){
        var v = obj[i], s;
        if(typeof v === 'object' && v){
          s = encodeURIComponent('"'+i.replace('"', '''"')+'":')+phpEncode(v);
        }
        else{
          v = typeof v === 'string' ? '"'+v.replace('"', ''"')+'"' : v;
          s = encodeURIComponent('"'+i.replace('"', '''"')+'":'+v);
        }
        r.push(s);
      }
    }
    return '%7B'+r.join(',')+'%7D';
  }
  else{
    r = typeof obj === 'string' ? '"'+obj.replace('"', '''"')+'"' : obj;
    return ''+r;
  }
}
function phpAccept(respText){
  return eval('('+decodeURIComponent(respText)+')');
}
function clone(obj){
  return phpAccept(phpEncode(obj));
}
function post(send, where, success, context){
  var x = new XMLHttpRequest || new ActiveXObject('Microsoft.XMLHTTP');
  var c = context || this;
  x.open('POST', where); x.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
  x.onreadystatechange = function(){
    if(x.readyState === 4 && x.status === 200){
      if(success)success.call(c, phpAccept(x.responseText));
    }
  }
  if(send && typeof send === 'object' && !(send instanceof Array)){
    var r = [];
    for(var p in send){
      r.push(encodeURIComponent(p)+'='+phpEncode(send[p]));
    }
    if(r.length)x.send(r.join('&'));
  }
  return x;
}
post({testProperty:'testValue'}, 'yourPage.php', function(response){
  // evaluate response and run all your other code here
});