简体   繁体   English

For 循环在 javascript 和 PHP 中无法正常工作

[英]For loop is not working properly in javascript with PHP

I am working with php and ajax,I am getting ajax response( as ["463","500","505"] ) and i am trying to pass this via loop but loop is working only single time instead of three Here is my code,Where i am wrong?我正在与 php 和 ajax 合作,我得到 ajax 响应(因为 ["463","500","500","505"] 在这里只尝试通过这个循环而不是循环来传递三个时间)我的代码,我哪里错了?

$.ajax({
    type: "post",
    url:'<?php echo base_url();?>Main/GetFollowingCoinUsers',
    data: {coin_id:coin_id},
    success: function (datan) {
        //datan contaning values like - ["463","500","505"]
        var str_array = datan.split(',');
        var conn = new WebSocket('ws://localhost:8282');
        var client = {
            user_id: <?php echo $number; ?>,
            recipient_id: null,
            NotificationType: 'notification',
            type: 'socket',
            token: null,
            message: null
        };

        conn.onopen = function (e) {
            conn.send(JSON.stringify(client));
        };
            
        for (var i=0; i < str_array.length; i++) {
            client.message = 'notifications';
            client.token = '';
            client.type = 'chat';
            client.recipient_id = str_array[i].send(str_array.toString());
            conn.send(JSON.stringify(client));  
        }
    }
});

You need to put client variable inside loop as well so that It can get all client id's one-by-one and send them notification您还需要将client变量放入循环中,以便它可以一一获取所有客户端 ID 并向它们发送通知

$.ajax({
        type: "post",
        url:'<?php echo base_url();?>Main/GetFollowingCoinUsers',
        data: {coin_id:coin_id},
        success: function (datan) {
            
            var conn = new WebSocket('ws://localhost:8282');
            for (var i=0; i < datan.length; i++) {
                var client = {
                    user_id: datan[i],
                    recipient_id: datan[i].send(datan.toString()), //code looks like a bit fishy, so pls check and correct it by yourself
                    NotificationType: 'notification',
                    type: 'chat',
                    token: '',
                    message: 'notifications'
                };
                conn.send(JSON.stringify(client));  
            }
        }
    });

Note: recipient id code looks like a bit fishy, so pls check and correct it by yourself注意: recipient id码看起来有点可疑,请自行检查并更正

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

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