代码之家  ›  专栏  ›  技术社区  ›  Andrés Bolívar

AJAX请求未收到我想要的属性

  •  -2
  • Andrés Bolívar  · 技术社区  · 2 年前

    我必须向以下API发送AJAX请求: https://jsonplaceholder.typicode.com/posts 。然后按“title”属性(升序)对结果进行排序,并通过控制台打印。日志

    这里的问题是,我没有得到title属性,而是得到了很多:

    Website Example, AJAX REQUEST, button, fetch API

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
    </head>
    <body>
        <div>
            <h1>Getting Started With Fetch API</h1>
            <button id="fetchJsonData">Fetch User Data</button>
        </div>
        <hr>
        <div id="response"></div>
    </body>
    <script>
        document.getElementById('fetchJsonData').addEventListener('click', fetchJson);
    
        function fetchJson(){
            fetch('https://jsonplaceholder.typicode.com/posts')
                .then(response => response.json())
                .then(json => console.log(json))
        }
        document.getElementById('fetchJsonData').addEventListener('click', fetchJson);
        
            function fetchJson(){
                fetch('https://jsonplaceholder.typicode.com/posts')
                    .then(response => response.json())
                    .then(titles => {
                        let output = '<h2>Lists of Titles</h2>';
                        // output += '<ul>';
                        titles.forEach(function(titles) {
                            output += `
                                <li>
                                    ${titles}
                                </li>
                            `;
                            output += '</ul>'
                        document.getElementById("response").innerHTML = output;
                        });
                        
                    });
            }
    
            function sortList(ul) {
      var ul = document.getElementById(ul);
    
      Array.from(ul.getElementsByTagName("LI"))
        .sort((a, b) => a.textContent.localeCompare(b.textContent))
        .forEach(li => ul.appendChild(li));
    }
    
    sortList("response");
            
    </script>
    </html>
            
    

    我是否以错误的方式获取数据?请给出建议,提前谢谢

    3 回复  |  直到 2 年前
        1
  •  1
  •   Cesare Polonara    2 年前

    响应是一个对象数组,如果要直接渲染,需要获取标题的字符串值。

    document.getElementById('fetchJsonData').addEventListener('click', fetchJson);
    
    function fetchJson() {
      fetch('https://jsonplaceholder.typicode.com/posts')
        .then(response => response.json())
        .then(json => console.log(json))
    }
    document.getElementById('fetchJsonData').addEventListener('click', fetchJson);
    
    function fetchJson() {
      fetch('https://jsonplaceholder.typicode.com/posts')
        .then(response => response.json())
        .then(titles => {
          let output = '<h2>Lists of Titles</h2>';
          output += '<ul>';
          titles.sort((a, b) => a.title.localeCompare(b.title)).forEach((obj) => {
            output += `<li>${obj.title}</li>`;
          });
          output += '</ul>';
    
          document.getElementById("response").innerHTML = output;
        });
    }
    <div>
      <h1>Getting Started With Fetch API</h1>
      <button id="fetchJsonData">Fetch User Data</button>
    </div>
    <hr>
    <div id="response"></div>
        2
  •  1
  •   Michael Pimentel    2 年前

    在forEach循环中,${titles}应该是${titles.title}

        3
  •  0
  •   harpal    2 年前

    这样做。

    阅读 docs 了解更多信息。

    document.getElementById('fetchJsonData')
        .addEventListener('click', fetchJson);
    
    function fetchJson(){
                fetch('https://jsonplaceholder.typicode.com/posts')
                    .then(response => response.json())
                    .then(posts => {
                        let output = '<h2>Lists of Titles</h2>';
                        posts.sort((a, b) => a.title.localeCompare(b.title));
                        posts.forEach(function(post) {
                            output += `
                                <li>
                                    ${post.title}
                                </li>
                            `;
                            output += '</ul>'
                        document.getElementById("response").innerHTML = output;
                        });
                        
                    });
            }
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
    </head>
    <body>
        <div>
            <h1>Getting Started With Fetch API</h1>
            <button id="fetchJsonData">Fetch User Data</button>
        </div>
        <hr>
        <div id="response"></div>
    </body>
    </html>