Search code examples
javascriptjqueryhtmlxmlhttprequest

Use JSON-value from one httprequest in another httprequest


Im trying to combine open data with 2 http-requests. I need to use one value of the first response, to make the second request-url (the value is part of the second url). How can i do that?

here is my code: https://codepen.io/1234cb/pen/wvBGKze

<html>
<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
</head>
<body>
    <h3>XMLHttpRequest</h3>
    zipcode <input type="text" id="zip" value="3136jr" title="zipcode"><br><br>
    housnr <input type="text" id="housenr" value="77" title="housenr"><br><br>
    <button type="button" onclick="loadDoc();loadDoc2()">Get Content</button>
    <p id="demo">response 1</p>
    <p id="demo2">response 2</p>

    <script>
        function loadDoc() {
            var xhttp = new XMLHttpRequest();
            xhttp.onreadystatechange = function() {
                if (this.readyState == 4 && this.status == 200) {
                    var myObj = JSON.parse(this.responseText);
                    document.getElementById("demo").innerHTML = myObj.response.docs[0].id;
                    //myObj.response.docs[0].id is the value/variable I need for the next httprequest
                }
            };
            xhttp.open("GET", "https://geodata.nationaalgeoregister.nl/locatieserver/v3/suggest?q=" + document.getElementById("zip").value + "+" + document.getElementById("housenr").value + "&wt=json", true);
            xhttp.send();
        };

        function loadDoc2() {
            var url = "https://geodata.nationaalgeoregister.nl/locatieserver/v3/lookup?id=adr-16dc4e7caee6f2b34222fb02b91a464e" //the value/variable myObj.response.docs[0].id should be the last part of the url (from "adr." to "64e")
            var vhttp = new XMLHttpRequest();
            vhttp.onreadystatechange = function() {
                if (this.readyState == 4 && this.status == 200) {
                    var Obj = JSON.parse(this.responseText);
                    document.getElementById("demo2").innerHTML = this.responseText;

                }
            };
            vhttp.open("GET", url, true);
            vhttp.send();
        };
    </script>
</body>
</html>

Solution

  • you have to pass id to loadDoc2 as :

    function loadDoc2(id) {        
    var url = "https://geodata.nationaalgeoregister.nl/locatieserver/v3/lookup?id="+id;//the value/variable myObj.response.docs[0].id should be the last part of the url (from "adr." to "64e")
    var vhttp = new XMLHttpRequest();
    vhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
    var Obj = JSON.parse(this.responseText);
    document.getElementById("demo2").innerHTML = this.responseText;
    
     }
     }; 
      vhttp.open("GET",url, true); 
      vhttp.send();
    };
    

    and call it on success of loadDoc:

    function loadDoc() {
    var xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function() {
      if (this.readyState == 4 && this.status == 200) {
      var myObj = JSON.parse(this.responseText);
      document.getElementById("demo").innerHTML = myObj.response.docs[0].id;
      loadDoc2(myObj.response.docs[0].id)
      //myObj.response.docs[0].id is the value/variable I need for the next httprequest
    }
     };
    
      xhttp.open("GET", "https://geodata.nationaalgeoregister.nl/locatieserver/v3/suggest?q=" + document.getElementById("zip").value +"+"+ document.getElementById("housenr").value +"&wt=json", true);
    xhttp.send();
      };