如何在现在无法使用的浏览器上运行Ajax代码?

时间:2019-02-15 21:38:39

标签: javascript php json ajax codeigniter

它可以在localhost上运行,但不能在实时主机上运行 请帮助此代码 我的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>

它可以在localhost上运行,但不能在实时主机上运行 请帮助此代码 代码中的错误或可能导致文件js受此代码影响

1 个答案:

答案 0 :(得分:1)

首先检查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>

如果这无济于事,请在此处留下错误消息吗?