Javascript xmlhttp响应文本

Javascript xmlhttp响应文本,javascript,php,html,ajax,Javascript,Php,Html,Ajax,我试图在不重新加载整个页面的情况下更新网页的特定div。而是在响应时,将完整的网页插入到targetid中。我正在使用javascript函数来实现这一点。下面是我调用javascript函数的php代码 if($check_like==0) { // if user has not liked this post $divid_like

我试图在不重新加载整个页面的情况下更新网页的特定div。而是在响应时,将完整的网页插入到targetid中。我正在使用javascript函数来实现这一点。下面是我调用javascript函数的php代码

if($check_like==0)
                        {
                            // if user has not liked this post
                            $divid_like = $postid.'like';
                            echo "<div id='$divid_like'><a href='#' onClick=\"ajax_post('Ajax.php?action=Like-Status','postid=$postid','$divid_like',0);\" name='Like'> Like </a> </div>";
                            //echo "<a href='action.php?action=Like-Status&postid=$postid' name='Unlike'> Like </a>";
                            echo "<a href='#' onClick='' > Comment </a>";
                            echo "<a href='#' onClick='' > Share </a>";
                        }
if($check\u like==0)
{
//如果用户不喜欢这个帖子
$divid_like=$posted'like';
回声“;
//回声“;
回声“;
回声“;
}
下面是我的javascript函数:

<script language="JavaScript">

function ajax_post(url , args , targetid, add)
{
    var xmlhttp;
    if(window.XMLHttpRequest)
    {
        // works for all browser abve IE7, chrome and firefox
        xmlhttp = new XMLHttpRequest();
    }
    else
    {
        //for IE5 and IE6 which i don't think i nedd to handle still
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }

    if(!xmlhttp)
        document.getElementById(targetid).innerHTML = "somesdlkvnoisnvoanfvnfnerroor";

    xmlhttp.open("POST",url, false);
    xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    //xmlhttp.setRequestHeader("Content-length", args.length);
    //xmlhttp.setRequestHeader("Connection", "close");

    xmlhttp.onreadystatechange=function()
    {
        var xhr = event.target;
        if(xhr.readyState==4 && xhr.status==200)
        {
            if(add==0)
            {
                document.getElementById(targetid).innerHTML = xhr.responseText;
            }
            else
            {
                document.getElementById(targetid).innerHTML+= xhr.responseText;
            }
        }
        else
        {
            document.getElementById(targetid).innerHTML = "somebigbigbigrroor";
        }

    };

    xmlhttp.send(args);
}

函数ajax_post(url、args、targetid、add)
{
var-xmlhttp;
if(window.XMLHttpRequest)
{
//适用于所有浏览器abve IE7、chrome和firefox
xmlhttp=新的XMLHttpRequest();
}
其他的
{
//对于IE5和IE6,我认为我仍然不需要处理它们
xmlhttp=新的ActiveXObject(“Microsoft.xmlhttp”);
}
如果(!xmlhttp)
document.getElementById(targetid.innerHTML=“somesdlkvnoisnvoanfnErroror”;
open(“POST”,url,false);
setRequestHeader(“内容类型”,“应用程序/x-www-form-urlencoded”);
//setRequestHeader(“内容长度”,args.length);
//setRequestHeader(“连接”,“关闭”);
xmlhttp.onreadystatechange=函数()
{
var xhr=event.target;
如果(xhr.readyState==4&&xhr.status==200)
{
如果(加法==0)
{
document.getElementById(targetid).innerHTML=xhr.responseText;
}
其他的
{
document.getElementById(targetid).innerHTML+=xhr.responseText;
}
}
其他的
{
document.getElementById(targetid.innerHTML=“somebigroor”;
}
};
xmlhttp.send(args);
}

正在调用Ajax.php文件以处理数据:

<?php

include_once("Includes/master_login.inc");
include_once("Includes/connect_to_database.php");
include_once("Includes/Users.php");
include_once("Includes/Friends.php");
include_once("Includes/current_page_url.php");
include_once("JavaScript/XML.js");
include_once("profile_display.php");
include_once("Includes/Status_Likes_Comment.php");
include_once("Includes/message.php");
include_once("Includes/NewsFeed.php");
include_once("sendmessage.php");
$action = $_GET["action"];
$uid = $_SESSION['uid'];

//if(!user_online($uid)){$action=0;}

if($action=="Add-Friend")   { add_friend($uid,$_POST["uid"]); if($_POST["refresh"]="profile") echo display_profile($_POST["uid"]); }    
if($action=="Respond-Friend-Request")   {friend_accept($_POST["uid"],$uid); if($_POST["refresh"]="profile") echo display_profile($_POST["uid"]); }
if($action=="Like-Status")  { like_status($uid,$_POST["postid"]);   echo "Unlike"; }
if($action=="Chat-Send")    {add_message($uid,$_POST["to"],$_POST["message"]); echo display_message($uid,$_POST["to"]);}
//if($action=="Chat-Send") {echo $_POST["message"]; delay(1000);}
    ?>

在这个div中加载喜欢这个状态而不是被不喜欢的完整页面替换时