亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 await/async 函數 fetch 顯示數據

使用 await/async 函數 fetch 顯示數據

PHP
千萬里不及你 2023-05-26 09:24:47
我正在為(我猜)一個簡單的問題尋求幫助。我一直在尋找這個答案,但我沒有找到這個問題的任何答案。我很難找到一種方法來選擇數組中的 2 個電子郵件地址和 2 條消息并將它們顯示在 div 中這個想法是有這個演示文稿:電子郵件 1 消息 1電子郵件 2 消息 2這是我的 php:<?php session_start();try{  $bdd = new PDO('mysql:host=localhost;dbname=XXX;charset=UTF8;', 'root', '');  }catch(Exception $e){  die('Erreur : '.$e->getMessage());}$message = $bdd->prepare("SELECT emailaddress, message FROM messages");$message->execute();$results = $message->fetchAll(PDO::FETCH_ASSOC);$json = json_encode($results);header("content-type:application/json");echo $json;exit(); ?>這是我的 JSON:    async function updateDisplay() {        const reponse = await fetch (url);        const data = await reponse.text();        const dataObject = JSON.parse(data);      console.log(dataObject);}updateDisplay()以及我的控制臺中的結果:0: "[email protected]"1: "test message" email: "[email protected]"message: "[email protected] test message"<prototype>: Object { … }1: {…}0: "[email protected]"1: "message 2"email: "[email protected]"message: "message test 2"<prototype>: Object { … }非常感謝你的幫助
查看完整描述

1 回答

?
MMTTMM

TA貢獻1869條經驗 獲得超4個贊

這是我的問題的解決方案:


    async function updateDisplay() {

        const response = await fetch ('http://localhost/xxxxx/fetch.php');

        const data = await response.text();

        const dataObject = JSON.parse(data);


        console.log(dataObject);


      let li = `<tr><th></th> <td> </td> <th></th></tr>`; 

        


        dataObject.forEach(user => { 

            li += `<tr> 

                <td>${user.email} </td> 

                <td>${user.message}</td>          

            </tr>`; 

        }); 

   


    document.getElementById("chat").innerHTML = li; 

 }

   

setInterval(updateDisplay, 1000);


查看完整回答
反對 回復 2023-05-26
  • 1 回答
  • 0 關注
  • 246 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號