javascript - 我如何在现在无法运行的浏览器上运行 ajax 代码?

标签 javascript php json ajax codeigniter

它适用于本地主机但不适用于实时托管 请帮助这个代码 我的 html 是这样的,

<div class="msg_box" style="left:5px">
  <div class="msg_head">chat

  </div>
  <div class="msg_wrap">
    <div class="msg_body" id="msg_body">
         <div  id="display_comment"></div>
         <div  id="msg"></div>
    </div>
     <form method="POST" id="comment_form">

     <input type="hidden" name="comment_name" id="comment_name" class="form-control" value="<?php echo $this->session->userdata('name') ?>" />


     <input type="hidden" name="idprod" id="idprod" class="form-control" value="<?php echo $prod_view->id; ?>" />
  <input type="hidden" name="comment_id" id="comment_id" value="0" />
  <div class="msg_footer"><textarea class="msg_input" name="comment_content" id="comment_content" rows="2"></textarea> <input type="submit" name="submit" id="submit" class="btn btn-info" value="Submit" /></div>
     </form>
</div>
</div>

jquery代码如下

    <link href='http://fonts.googleapis.com/css?family=Source+Sans+Pro|Open+Sans+Condensed:300|Raleway' rel='stylesheet' type='text/css'>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
   <script>
$(document).ready(function(){


$('#comment_content').keypress(function (eventt) {

  if (e.which == 13) {
    $('#submit').submit();
    return false;    //<---- Add this line
  }
});


 $('#comment_form').on('submit', function(event){
  event.preventDefault();
  var form_data = $(this).serialize();

  $.ajax({
   url:"<?php echo base_url(); ?>" + "index.php/home/add_comment/",
   method:"POST",
   data:form_data,
   dataType:"JSON",
   success:function(data)
   {
    if(data.error != '')
    {
     $('#comment_form')[0].reset();
     $('#comment_message').html(data.error);
     $('#comment_id').val('0');

     load_comment();

    }
    $('#msg_body').animate({scrollTop: 6000000}, 600);


   }
  })
 });



$(document).ready(function() {
    setInterval('load_comment', 5000);

});
 load_comment();




var RefreshTimerInterval = 1000;

function load_comment()
 {

  $.ajax({
   url:"<?php echo base_url(); ?>" + "index.php/home/fetch_comment/",
   method:"POST",
    data: {
      'idprod': $('#idprod').val()

                },
   success:function(data)
   {
    $('#display_comment').html(data);
  setTimeout(load_comment, RefreshTimerInterval);
   }

  })
 }
$(document).ready(function() {
    setInterval('load_comment', 500);
});
 $(document).on('click', '.reply', function(){
  var comment_id = $(this).attr("id");
  $('#comment_id').val(comment_id);
  $('#comment_name').focus();
 });

});



</script>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script>
    var RefreshTimerInterval = 1000; // every 5 seconds
    $(document).ready(getData);

    function getData() {
        $.get('<?php echo base_url(); ?>' + 'index.php/home/fetch_comment', function(data) {
             $('#display_comment').html(data);

             setTimeout(getData, RefreshTimerInterval);
        }
    }
</script>

它适用于本地主机但不适用于实时托管 请帮助这个代码 是代码中的错误还是可能有文件js受此代码影响

最佳答案

首先检查 config 文件夹中的 config.php 文件并确保第 26 行是 $config['base_url'] = ' http://www.yourwebsite.com ';

我的建议是这样使用 base_url:

<link href='http://fonts.googleapis.com/css?family=Source+Sans+Pro|Open+Sans+Condensed:300|Raleway' rel='stylesheet' type='text/css'>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
   <script>
$(document).ready(function(){


$('#comment_content').keypress(function (eventt) {

  if (e.which == 13) {
    $('#submit').submit();
    return false;    //<---- Add this line
  }
});


 $('#comment_form').on('submit', function(event){
  event.preventDefault();
  var form_data = $(this).serialize();

  $.ajax({
   url:"<?php echo base_url('index.php/home/add_comment'); ?>",
   method:"POST",
   data:form_data,
   dataType:"JSON",
   success:function(data)
   {
    if(data.error != '')
    {
     $('#comment_form')[0].reset();
     $('#comment_message').html(data.error);
     $('#comment_id').val('0');

     load_comment();

    }
    $('#msg_body').animate({scrollTop: 6000000}, 600);


   }
  })
 });



$(document).ready(function() {
    setInterval('load_comment', 5000);

});
 load_comment();




var RefreshTimerInterval = 1000;

function load_comment()
 {

  $.ajax({
   url:"<?php echo base_url('index.php/home/fetch_comment'); ?>",
   method:"POST",
    data: {
      'idprod': $('#idprod').val()

                },
   success:function(data)
   {
    $('#display_comment').html(data);
  setTimeout(load_comment, RefreshTimerInterval);
   }

  })
 }
$(document).ready(function() {
    setInterval('load_comment', 500);
});
 $(document).on('click', '.reply', function(){
  var comment_id = $(this).attr("id");
  $('#comment_id').val(comment_id);
  $('#comment_name').focus();
 });

});



</script>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script>
    var RefreshTimerInterval = 1000; // every 5 seconds
    $(document).ready(getData);

    function getData() {
        $.get("<?php echo base_url('index.php/home/fetch_comment'); ?>", function(data) {
             $('#display_comment').html(data);

             setTimeout(getData, RefreshTimerInterval);
        }
    }
</script>

如果这没有帮助,请在此处留下错误消息好吗?

关于javascript - 我如何在现在无法运行的浏览器上运行 ajax 代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54717408/

相关文章:

php - 我应该调用一个包含 SESSION 值的变量而不是在 PHP 中调用实际 session 吗?

php - 将图像从android上传到PHP服务器

php - session 与 URI 多语言标识符

javascript - 接收由 jQuery 发送并由 Node Js 接收的 JSON 形式的数组表单数据

javascript - 如何让 if 条件看起来更简洁?

javascript - Jquery ('#iframeid' ).load() 在加载 Iframe 内容(文本、图像)之前执行

javascript - SVG 的自由变换插件 - 创建占位符元素

javascript - 在 React 中切换布局同时保持状态

ios - 在 iOS 上解析 json 数组

MySQL Workbench 支持 json 列