最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - GET xmlHTTPrequest not working - Stack Overflow

programmeradmin1浏览0评论

i am trying to make an ajax menting system where if a new ment is posted, the document title changes to (1) website title (like twitter)

my code is here

The xmlHTTPrequest

 function loadXMLDoc7(url)
        {
        if (window.XMLHttpRequest)
          {// code for IE7+, Firefox, Chrome, Opera, Safari
          xmlhttp=new XMLHttpRequest();
          }
        else
          {// code for IE6, IE5
          xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
          }
        xmlhttp.open("GET",url,false);
        xmlhttp.send(null);
        document.getElementById('newments').innerHTML=xmlhttp.responseText;
        }

The PHP

    echo "<script type='text/javascript'>
    function auto2ments()
    { 
    var MyDiv1 = document.getElementById('uiuiui');";
    echo "loadXMLDoc7(MyDiv1.innerHTML)";
    echo "}";
    echo "setInterval(\"auto2ments()\",15000);</script>";
    }

The DIV uiuiui contains /newmentingi.php?show=0&id=username The problem is when the Newments DIV gets filled, it shows
ID =
Show = 0
why?

i am trying to make an ajax menting system where if a new ment is posted, the document title changes to (1) website title (like twitter)

my code is here

The xmlHTTPrequest

 function loadXMLDoc7(url)
        {
        if (window.XMLHttpRequest)
          {// code for IE7+, Firefox, Chrome, Opera, Safari
          xmlhttp=new XMLHttpRequest();
          }
        else
          {// code for IE6, IE5
          xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
          }
        xmlhttp.open("GET",url,false);
        xmlhttp.send(null);
        document.getElementById('newments').innerHTML=xmlhttp.responseText;
        }

The PHP

    echo "<script type='text/javascript'>
    function auto2ments()
    { 
    var MyDiv1 = document.getElementById('uiuiui');";
    echo "loadXMLDoc7(MyDiv1.innerHTML)";
    echo "}";
    echo "setInterval(\"auto2ments()\",15000);</script>";
    }

The DIV uiuiui contains /newmentingi.php?show=0&id=username The problem is when the Newments DIV gets filled, it shows
ID =
Show = 0
why?

Share Improve this question edited Jul 19, 2013 at 15:29 tlenss 2,6092 gold badges23 silver badges26 bronze badges asked Jul 19, 2013 at 15:18 user2166538user2166538 3131 gold badge3 silver badges18 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 1

The XmlHttpRequest object is asynchronous, which means that when it has the data ready, it returns it in a method. It is best to create a function to act as an event handler so that when the server responds, it calls the event handler function.

I think the solution you need is similar to here: How to get the response of XMLHttpRequest?

发布评论

评论列表(0)

  1. 暂无评论