jQuery-Ajax未在实时服务器上运行

时间:2020-09-09 00:54:06

标签: jquery codeigniter

大家好!

请尝试通过CodeIgniter构建消息应用程序,突然发现我的Jquery代码无法在实时服务器上运行。

相同的代码片段在我的本地主机上运行良好。

这是我打开JS控制台时看到的:

f.send(a.hasContent && a.data || null),

这是jQuery代码:

$("#chats").submit(function(e){
        e.preventDefault();
        var message = $(".max").val();;
        var user_id = $("#user_id").val();
        var receiver = $("#receiver").val();
        //var chat_alert = document.getElementById('chat_alert');
        $.ajax({
            type: "POST",
            url: '<?php echo base_url('pages/chats') ?>',
            data: $("#chats").serialize(),
            dataType: "json",
            beforeSend: function () {
              $('#send').html("<img src='<?php echo base_url()?>assets/images/b.gif' width='20' height='20'/>");
            },
            success:function(result)
            {
              if (result.error) {
                if (result.msg_error) {
                  $('#send').html('<i class="fa fa-paper-plane-o"></i>');
                  $("#msg-error").show().attr('class', 'alert alert-info').html(result.msg_error);
                }else if(result.activation){
                  $('#send').html('<i class="fa fa-paper-plane-o"></i>');
                  $("#error").attr('class', 'alert alert-info').html('<i class="fa fa-info"></i> You have reached the maximum free message for '+receiver+'. ');
                }else if(result.failed){
                  $('#send').html('<i class="fa fa-times"></i>');
                  $("#error").attr('class', 'text-danger').html('Failed');
                }
              }else if (result.success) {
                load_messages_active();
                $('#send').html('<i class="fa fa-paper-plane-o"></i>');
                $("#success").show();
                $("#progress").hide();
                document.getElementById("chats").reset();
              }
            },
            error:function()
            {
              $('#send').html('<i class="fa fa-times"></i>');
              $("#error").attr('class', 'text-danger').html('Something went wrong...');
            }
          });
        });

这是视图:

<form id="chats" method="post" action="javascript:void(0)">
   <p id="error"></p>
   <div class="input-group col-md-12">
     <textarea class="form-control col-md-12 chat-field" maxlength="500" id="max" name="message" spellcheck="true"></textarea>
   <label class="input-group-addon chat-field-btn">
      <button type="submit" name="submit" class="btn btn-default" id="send"><i class=" fa fa-paper-plane-o"></i></button>
   </label>
   <label id="msg-error text-danger"></label>
   <input type="hidden" name="user_id" value="<?=$val->ref_id;?>" id="user_id"/>
   <input type="hidden" name="receiver" value="<?=$val->name;?>" id="receiver"/>
</div>
   <div class="col-md-12">
    <span id="counter" class="pull-right col-md-6"></span>
   </div>
</form?>

这是我的控制器:

        {
            $this->form_validation->set_rules('message', 'Message', 'required');

            $to = $this->input->post('user_id');
            $message = $this->input->post('message');

            if ($this->session->userdata('username')):
                $session_data = $this->session->userdata('username');
                $query = $this->db->query("select * from account where email='$session_data'");
                $data = $query->result();
                foreach ($data as $key => $value);
                $from = $value->ref_id;
                
                //check if the huecoin is still enough to allow the contact send message
                $stmt = $this->db->query("select * from payments where user_id='$from'");
                $coin = $stmt->result();
                foreach ($coin as $key => $val);

                $chats['message'] = $message;
                $chats['receiver'] = $to;
                $chats['sender'] = $from;
                $chats['date_sent'] = date('D. j F, Y h:m a');
                    
                if ($this->form_validation->run() == FALSE):
                    $array = array(
                        'error' =>true ,
                        'msg_error' => form_error('message') 
                    );
                else:
                    //check if the sender has sent message to receiver earlier

                    $chat = $this->db->query("select * from chats where receiver='$to' and sender='$from'");
                    $max_chat = $chat->num_rows();

                    if( (($val->activation > 0) && ($max_chat > 0)) // the receiver have received messages from the sender earlier and has activated his/her account
                        || (($val->activation > 0) && ($max_chat == 0))//the receiver have not received messages from the sender earlier and has activated his/her account
                        ):

                        $result = $this->Career_model->chat($chats);
                        if($result):
                            $array = array('success' => true, 'sent' => true);
                        else:
                            $array = array('error' => true, 'failed' => true);
                        endif;

                    elseif((($val->activation == 0) && ($max_chat <= 2))//the receiver have not received messages from the sender earlier and has not activated his/her account
                        ):
                        $result = $this->Career_model->chat($chats);
                        if($result):
                            $array = array('success' => true, 'activation' => true);
                        else:
                            $array = array('error' => true, 'failed' => true);
                        endif;
                    else://
                        $array = array('error' => true, 'activation' => true);
                    endif;
                endif;
            endif;
            echo json_encode($array);
        }```

Thanks in addy!

1 个答案:

答案 0 :(得分:0)

谢谢,伙计们!

问题出在存储聊天的列中……

我已修复它。上面的代码没有错误。

干杯!

相关问题