简体   繁体   English

Ajax:如何/是否可以解析“.html(响应)”

[英]Ajax : How/ Is it possible to parse a '.html(response)'

so am making this chat, and am trying to add functionalities.所以我正在做这个聊天,并试图添加功能。 SO far, I have here is what i achieved.到目前为止,我在这里所取得的成就。

I have a function that displays messages.我有一个显示消息的 function。

function displayMessages()
    {
        $.post('core/chat.php?action=read',function(response)
        {
            $('.message_display').html(response);
            
        });
}

this function is called every second每秒调用此 function

setInterval(function(){displayMessages();},1000);

the content of the 'reponse' is generated in php. “响应”的内容在 php 中生成。

foreach($messages as $chat_msg)
    {
        echo '<span style="color:#'.$chat_msg['color'].';">['.$chat_msg['user_name'].']:'.$chat_msg['txt'].' </span><br>';              
    }

so i build a function that play sound: playsound(file).所以我构建了一个播放声音的 function:playsound(file)。 I initially wanted to attach it to the function displayMessages()...我最初想将它附加到 function displayMessages()...

function displayMessages()
{
        $.post('core/chat.php?action=read',function(response)
        {
            $('.message_display').html(response);

        });
        playSound ("send_message");

}

...but of course, that plays the sound at each refresh, having a new message or not. ...但当然,它会在每次刷新时播放声音,无论是否有新消息。 So,to fix it, i thought of adding on the php sidee, a code that triggers the sounds if a new message was posted: "ex:所以,为了修复它,我想在 php sidee 上添加一个代码,如果发布了新消息就会触发声音:“例如:

echo $playsoundtrigger="playsound";

but then i have no clue, on what to do next.但后来我不知道下一步该做什么。 how can i parse response, so the ajax break down the html on 1 side and the $playsoundtrigger on the other?我如何解析响应,所以 ajax 在一侧分解 html 而在另一侧分解 $playsoundtrigger? something along the line i would have approach like this if it was php:如果它是 php,我会采用这样的方法:

parse_str("reponse+$triger=true");
if ($triger){
   playsound();
}

I look for a parse function in ajax, but all reference i could find was to parse json... any solution?我在 ajax 中寻找解析 function,但我能找到的所有参考都是解析 json ......任何解决方案? or hint on a logic to address my problem?或暗示解决我的问题的逻辑?

Note: I looked, and find that ajax, could tell me if the content has changed, based on his side, but I do not want to go this way, as I would like in the long run, to adapt the sounds on the message:)注意:我看了看,发现ajax,可以告诉我内容是否发生了变化,基于他的一面,但我不想go这样,从长远来看,我想适应消息上的声音:)

thank you谢谢你

Polling your server on every second... Hm... you are essentially DDOS ing your own server.每秒轮询您的服务器...嗯...您实际上是在对自己的服务器进行 DDOS。 I think web sockets are what you need.我认为 web sockets 是您所需要的。 But don't know much about it.但对它了解不多。

As for your answer, you could actually use json, then build the nodes from browser.至于您的答案,您实际上可以使用 json,然后从浏览器构建节点。

Eg:例如:

$data = [];

foreach ($messages as $chat_msg) {
   $data[] = [
        'color' => $chat_msg['color'],
        'user_name' => $chat_msg['user_name'],
        'txt' => $chat_msg['txt']
   ];
}

echo json_encode([
    'trigger' => 1
    'data' => $data
]);

and in your js在你的 js 中

function displayMessages()
{
    $.post(
        'core/chat.php?action=read',
        {},
        function(res) {
            var html = '';
            res.data.foreach(function(msg) {
                html += '<span style="color:#' + msg.color + ';">[' + msg.user_name + ']:' + msg.txt + '</span><br>';
            }
            $('.message_display').html(html);
            if(res.trigger) {
                 playSound ("send_message");
            }
        },
        'json'
    );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM