javascript - 如何在现在无法运行的浏览器上运行 ajax 代码?
问题描述
它适用于本地主机,但不适用于实时托管请帮助处理此代码,我的 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 受此代码影响
解决方案
首先检查 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>
如果这没有帮助,您可以在此处留下错误消息吗?
推荐阅读
- flutter - 您能否检测到 PageView 的页面何时不再可见?
- impala - 在 Impala 3.4.0 中创建表时,Kudu 错误中不支持类型 VARCHAR(n)
- python - Python - 将 SVG 从磁盘加载到 matplotlib 子图中,然后将结果以 PDF 格式写入磁盘
- javascript - 如何在一周内获得一个订单日期并使其以 MM-DD-YYY 格式显示,而不是使用 mongoldb 的 ISO 格式
- php - 存储过程和 REST API 作为事务调用
- google-apps-script - 阻止用户在 google sheet 中书写
- amazon-web-services - AWS 在现有网站之上添加身份验证层
- docker-compose - 将操作系统添加到 docker-compose 文件
- ios - 如何根据iOS版本选择性导入Framework并实现扩展?
- angular - 当 Id 匹配时,角度 ngrx 状态数组项更新