Tell me, please, how can I pass an object on pure JS, without any jQuery by means of AJAX?

UPD: why does a 405 error occur?

Added to request:

   xhr.setRequestHeader ('Content-type', 'application / x-www-form-urlencoded');
   xhr.setRequestHeader ('Access-Control-Allow-Origin', '*');
   xhr.setRequestHeader ('Access-Control-Allow-Headers', 'X-Requested-With');
   xhr.setRequestHeader ('Access-Control-Allow-Methods',' PUT, GET, POST, OPTIONS ');'

But it did not help.

  • send where? which object? - Grundy
  • A normal JS object, including 4 string properties, per server. - Timur Musharapov
  • As background information, a lot about xmlhttprequest, if you 're interested: xmlhttprequest.ru - Alex Krass

3 answers 3

Example:

var data = ... var method = "POST" // или GET var url = ... httpReq(url, method, data, function(res) { console.log("response: ", res); }); function getXmlHttp() { var xmlHttp = null; if (window.XMLHttpRequest) { xmlHttp = new XMLHttpRequest(); } else if (window.ActiveXObject) { xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); } return xmlHttp; } function httpReq(URL, method, data, success, error) { var request = getXmlHttp(); request.open(method, URL, true); request.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); // application/x-www-form-urlencoded - для передачи urlencoded данных // Для FormData использовать multipart/form-data request.send(data); request.onreadystatechange = function() { if (request.readyState == 4) { if (request.status == 200) { success(request.responseText); } else { if (error) error(request.status); } } } } 

    There is such a constructor XMLHttpRequest, you can read for example here - https://learn.javascript.ru/ajax-xmlhttprequest . Example of use, taken from the article on the link:

     / 1. Создаём новый объект XMLHttpRequest var xhr = new XMLHttpRequest(); // 2. Конфигурируем его: GET-запрос на URL 'phones.json' xhr.open('GET', 'phones.json', false); // 3. Отсылаем запрос xhr.send(); // 4. Если код ответа сервера не 200, то это ошибка if (xhr.status != 200) { // обработать ошибку alert( xhr.status + ': ' + xhr.statusText ); // пример вывода: 404: Not Found } else { // вывести результат alert( xhr.responseText ); // responseText -- текст ответа. } 

    • I would have a good example. - Timur Musharapov
    • There is a search. There are a million examples on the site - zhenyab
    • He, unfortunately, only jQuery issues. - Timur Musharapov
    • Have mercy, my friend. We are looking for the word XMLHttpRequest - zhenyab
     var xhr = new XMLHttpRequest(); obj={ param1:"1", param2:"2" } xhr.open('POST',"/ajaxtest.php", false); xhr.send(obj); if (xhr.status != 200) { alert( xhr.status + ': ' + xhr.statusText ); } else { alert( xhr.responseText ); // responseText -- текст ответа. } 
    • pass an object instead of url? The second parameter of the open function is the address where the request is made - Grundy
    • And we send obj to send, I now understand. - Timur Musharapov
    • described simple. - test_q_1