A
A
Azat Kiberov2019-07-14 20:09:16
AJAX
Azat Kiberov, 2019-07-14 20:09:16

Why doesn't my code created on XMLHttpRequest work?

Here is the code

<script>
function Clickptpip(Element) {
var xhr = new XMLHttpRequest();
  
    xhr.open('POST', 'test2.php', true);
    xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  
  var body = 'url=' + encodeURIComponent(url) +
  '&url2=' + encodeURIComponent(url2) +
   '&urlsite=' + encodeURIComponent(urlsite) +
   '&text=' + encodeURIComponent(text) +
    '&referer=' + encodeURIComponent(referer) ;
  
  xhr.send(body);
  
  xhr.onreadystatechange = function() { // Ждём ответа от сервера
      if (xhr.readyState == 4) { // Ответ пришёл
        if(xhr.status == 200) { // Сервер вернул код 200 (что хорошо)
          document.getElementById("otvet").innerHTML = xhr.responseText; // Выводим ответ сервера
        }
      }
    };
  
}
</script>



<div>
<form>
  <input type="text" name="urlsite" id="a" value="test" />
  <br />
  <input type="text" name="b" id="b" />
  <br />
  <input type="button" value="Вывод" onclick="Clickptpip(this);" />
  </form>
  <p>Ответ сервера <span id="otvet"></span></p>
</div>

Tried to change and this way and that does not work. What's the matter?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question