0

just using vanilla javascript is it possible to send data to a php server without appending it to the url?

here's what I have so far:

function getAjax(url, success) {
    var xhr = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP');
    xhr.open('GET', url);
    xhr.onreadystatechange = function() {
        if (xhr.readyState>3 && xhr.status==200) success(xhr.responseText);
    };
    xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
    xhr.send();
    return xhr;
}

getAjax('http://localhost/login/public/login.php?email=someone@gmail.com', 
         function(data){ console.log(data); });

can I add $_GET parameters in xhr.send()? or is there another way to send this object along with the request:

data = {email:'someone@gmail.com',
        password:'compas'}
DCR
  • 10,658
  • 7
  • 38
  • 86
  • https://stackoverflow.com/questions/5999118/how-can-i-add-or-update-a-query-string-parameter – Quentin May 24 '21 at 23:25
  • "can I add $_GET parameters in xhr.send()? " — No. `$_GET` is populated from the query string in the URL, not the request body (which XMLHttpRequest can't add to a GET request). – Quentin May 24 '21 at 23:26
  • Why not use FETCH since it's now part of the JavaScript language API? – Chris May 24 '21 at 23:31
  • the fetch documentation is not very good. do you have a better reference then MDN – DCR May 24 '21 at 23:54
  • @Chris — It's what? What version of ECMAScript was that added in? – Quentin May 25 '21 at 08:54
  • @Quentin I would say in either late 2014 or early 2015. – Chris May 25 '21 at 21:17
  • @dcr Maybe this link would be a little better in documentation: https://developers.google.com/web/updates/2015/03/introduction-to-fetch a simpler page with FETCH documentation can be found here too: https://flaviocopes.com/fetch-api/ – Chris May 25 '21 at 21:18
  • @Chris — https://fetch.spec.whatwg.org/ is the fetch spec (and https://xhr.spec.whatwg.org/ is XHR) neither are part of the JavaScript language. They are web API extensions. – Quentin May 25 '21 at 21:19

0 Answers0