Home javascript How to make a GET request in pure JavaScript?

How to make a GET request in pure JavaScript?




How can you make a GET request through javascript and get a response from it (html code of a certain page)?

For example: site.ru/index.php site.ru/auth.php

You need to add a script to js in index.php so that it makes a get request to auth.php, and gets its source code.

I do this:

& lt; script type = "text / javascript" & gt;
var x = new XMLHttpRequest ();
x.open ("GET", "http://ya.ru/r=" + Math.random (), true);
x.onreadystatechange = function () alert (x.responseText);}
x.send (null);
& lt; / script & gt;

Answer 1, authority 100%

In pure JS, everything will work out. You have a typo: missed the “{“. And you need to catch the load event instead of onreadystatechange :

var x = new XMLHttpRequest ();
x.open ("GET", "/ echo / json /", true);
x.onload = function () {
  alert (x.responseText);
x.send (null);

Working example .

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions