Jump to content

JSON Http Request


Guest

Recommended Posts

<script>
                         
                       function getThem(id, url, a) {
                      var request = new XMLHttpRequest();
                      request.open("GET", url);
                      request.responseType = "json";
                      request.send();
                      request.onload = function() {
                        var jsonObj = request.result;
                        var tbody = document.querySelector(id);
                        var tr = document.createElement("tr");
                        arra = [
                          
                          jsonObj.result[a].Steam_ID,
                          jsonObj.result[a].ShowAsOffline
                        ];
                        for (p = 0; p < arra.length; p++) {
                          var td = document.createElement("td");
                          
                          
                          td.textContent = arra[p];
                          tr.appendChild(td);
                        }
                        tbody.appendChild(tr);
                      };
                    }
                    const tableApis = [
                      "my url"
      
                      
                    ];
                    for (let table = 0; table < 4; ++table) {
                      for (let a = 0; a < 6; a++) {
                        getThem("#table" + (table + 1), tableApis[table], a);
                      }
                    }
                          
                   
                          
                    </script>
           
              <table class="table">
                <thead>
                  <tr>
                    <th scope="col">1/th>
                    <th scope="col">2</th>
                    
                  </tr>
                </thead>
                <tbody id="table1">
                </tbody>
              </table>
            </div>
          </div>
        </div>
{
  "result": [
    {
      "User": {
        "UserName": "test",
        "InSped": true,
        "FormatName": "test"
      },
      "isTeamMember": false,
      "ShowAsOffline": false,
      "State": 0,
      "Steam_ID": 123,
      "Spotify_TitleURI": ""
    }
  ],
  "error": false,
  "msg": null,
  "version": 202010231
}

I get this errors https://prnt.sc/v9e7pd https://prnt.sc/v9e83o

Link to comment
Share on other sites

On 10/29/2020 at 5:54 PM, kicken said:


var jsonObj = request.result;

This sets jsonObj to undefined because XMLHttpRequest doesn't have a result property.  You need to read and parse the responseText property.

XMLHttpRequest is an older standard.  You might also want to look at the more modern fetch() API instead.

I changed it to 

var jsonObj = request.response;

but the table still doesn't show anything

Edited by Guest
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.