Search code examples
javascriptajaxgetxmlhttprequest

How to send data in 'GET' request of XMLHttpRequest in javascript?


and thanks for investing your time in this post.

I want to send a data in GET request in XMLHttpRequest.

function useAjax(url,data){
        const xhttp = new XMLHttpRequest();
        xhttp.onload = function(e) {
            const resData = JSON.parse(xhttp.responseText) ;
            setResponseData(true,resData)
        }
        xhttp.open("GET", url, false);
        xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        xhttp.send(data);
   }

I tried to make change on open function params like xhttp.open("GET", url, [false,data]);

but still getting no result


Solution

  • "Get" request has no body, to send some data use "POST"

    If the request method is GET or HEAD, the body parameter is ignored and the request body is set to null