dongsuyou6938 2013-09-28 23:56
浏览 37
已采纳

为什么AJAX POST不起作用?

can anyone tell me why my AJAX POST is not working... I added alert() and it is working... idk where is wrong here... this is my code...

<script language="JavaScript" type="text/javascript">
    function initialCalendar() {
        var hr = new XMLHttpRequest();
        var url = "calendar.php";
        var currentTime = new Date();
        var month = currentTime.getMonth() + 1;
        var year = currentTime.getFullYear();
        showmonth = month;
        showyear = year;
        var vars = "showmonth="+showmonth+"&showyear="+showyear;
        hr.open("POST", url, true);
        hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        hr.onreadystatechange = function() {
            if(hr.readyState == 4 && hr.status == 200) {
                var return_data = hr.responseText;
                document.getElementById("showCalendar").innerHTML = return_data;
            }
        }
        hr.send(vars);
        document.getElementById("showCalendar").innerHTML = "processing..";
        alert(vars);
    }

    function next_month(){
        var nextmonth = showmonth + 1;
        if(nextmonth > 12) {
            nextmonth = 1;
            showyear = showyear + 1;
        }
        showmonth = nextmonth;
        var hr = new XMLHttpRequest();
        var url = "calendar.php";
        var vars = "showmonth="+showmonth+"&showyear="+showyear;
        hr.open("POST", url, true);
        hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        hr.onreadystatechange = function() {
            if(hr.readyState == 4 && hr.status == 200) {
                var return_data = hr.responseText;
                document.getElementById("showCalendar").innertHTML = return_data;
            }
        }
        hr.send(vars);
        document.getElementById("showCalendar").innertHTML = "processing...";
        alert(vars);
    }

    function last_month(){
        var lastmonth = showmonth - 1;
        if(lastmonth < 1) {
            lastmonth = 1;
            showyear = showyear - 1;
        }
        showmonth = lastmonth;
        var hr = new XMLHttpRequest();
        var url = "calendar.php";
        var vars = "showmonth="+showmonth+"&showyear="+showyear;
        hr.open("POST", url, true);
        hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        hr.onreadystatechange = function() {
            if(hr.readyState == 4 && hr.status == 200) {
                var return_data = hr.responseText;
                document.getElementById("showCalendar").innertHTML = return_data;
            }
        }
        hr.send(vars);
        document.getElementById("showCalendar").innertHTML = "processing...";
        alert(vars);
    }
</script>

and the body is

<body onLoad="initialCalendar();">
<div id="showCalendar"> </div>
</body>

and the button I used to next and previous... this is from calendar.php

<?php
$showmonth = $_POST['showmonth'];
$showyear = $_POST['showyear'];
$showmonth = preg_replace('#[^0-9]#i', '', $showmonth);
$showyear = preg_replace('#[^0-9]#i', '', $showyear);

$day_count = cal_days_in_month(CAL_GREGORIAN, $showmonth, $showyear);
$pre_days = date('w', mktime(0,0,0, $showmonth, 1, $showyear));
$post_days = (6 - (date('w', mktime(0,0,0, $showmonth, $day_count, $showyear))));


echo '<div class="title_bar">';
echo '<div class="previous_month"><input name="myBtn" type="submit" value="Previous Month" onClick="javascript:last_month();"></div>';
echo '<div class="show_month">' . $showmonth . '/' . $showyear . '</div>';
echo '<div class="next_month"><input name="myBtn2" type="submit" value="Next Month" onClick="javascript:next_month();"></div>';
echo '</div>';
  • 写回答

1条回答 默认 最新

  • doufangpian5545 2013-09-29 00:25
    关注

    Change innertHTML, to innerHTML in your next_month() and last_month() functions. I suggest using an editor like Komodo Edit 7.1. (I think that's the latest free version for Windows. Not sure about Mac.)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数