将字符串添加到ajax responseText

时间:2016-04-13 20:01:00

标签: javascript ajax

我正在尝试在我的ajax responseText字符串中添加一个字符串。我已经检查过,是的,responseText是一个字符串,但是当我尝试在字符串中添加一些额外的文本时没有任何变化。我的js:

function ajax(instruction, push, url, callback) {

  var xmlhttp; // the object for the httprequest

  if (window.XMLHttpRequest) {
    // code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp = new XMLHttpRequest();
  } else {
    // code for IE6, IE5
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  }

  xmlhttp.onreadystatechange = function() {
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { // status 200 = sucessfull page! NOT 404!  // 1 2 3 4 are states of the request (4 is when it's done)

      //console.log(xmlhttp.responseText);

      xmlhttp.responseText = xmlhttp.responseText + "<script>resize();</script>";
      console.log(xmlhttp.responseText);
      callback(xmlhttp);
    }
  };
  xmlhttp.open(instruction, url, true); // sends a the var q to the next php file
  xmlhttp.send(''); // Sends the request

  if (push == true) { // Change the link to the url of the ajax with

    var newLink = "?p=" + url; // Gives us the link we want except that we don't want the .php

    newLink = newLink.substring(0, newLink.indexOf('.')); // makes a new string with character 0 to the dot

    var urlPath = window.location.protocol + "//" + window.location.host + window.location.pathname;
    window.history.pushState({
      path: urlPath
    }, '', newLink);

  } else {}

}
<a href='' onclick="if(useAjax == true){ ajax('GET', true, 'home.php', function(xmlhttp){ document.getElementById('content_holder').innerHTML = xmlhttp.responseText; }); return false;}">Home</a>

由于某些原因,代码片段在这里不起作用,但这一切都适用于我,除非执行此操作时响应文本没有任何反应:

xmlhttp.responseText = xmlhttp.responseText + "<script>resize();</script>";

0 个答案:

没有答案