dongxun7962 2013-07-19 19:40
浏览 152
已采纳

ajax回调函数为空

It's my first time on StackOverflow, please forgive me if I forget some important information or if my question sounds stupid!

I am building a web site which generates a list once the user writes a word and hits the "Send" button. Php is responsible for extracting the required data and returning it to the web page as a list.

My problem is that I then want to pass this data in a js function. I have read a lot of answers on StackOverflow concerning this, and it sounded like a callback function is what I needed. However, my js function keeps telling me that my node is null; it seems the data returned from ajax is not accounted for.

Here are some parts of my code:

<script>
        var xmlhttp;
        function loadXMLDoc(cfunc){
            if (window.XMLHttpRequest)  {// code for IE7+, Firefox, Chrome, Opera, Safari
              xmlhttp=new XMLHttpRequest();
            } else  {// code for IE6, IE5
              xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
            }
            var us = document.getElementById("user").value;
            var ur = document.getElementById("wiki").value;
            xmlhttp.onreadystatechange = cfunc;
            xmlhttp.open("GET","contributions_old.php?user="+us+"&wiki="+ur+"",true);
            xmlhttp.send();
        }
        function myFunction()   {
            loadXMLDoc(function() {                     
                if (xmlhttp.readyState==4 && xmlhttp.status==200) {
                    document.getElementById("result").innerHTML=xmlhttp.responseText;
                    var parser = new DOMParser ();
                    var responseDoc = parser.parseFromString (xmlhttp.responseText, "text/html");
                    var text1 = responseDoc.getElementById('old1').value;
                    var text2 = responseDoc.getElementById('new1').value;

                    var dmp = new diff_match_patch();
                    dmp.Diff_Timeout = 0;

                    // No warmup loop since it risks triggering an 'unresponsive script' dialog
                    // in client-side JavaScript
                    var ms_start = (new Date()).getTime();
                    var d = dmp.diff_main(text1, text2, false);
                    var ms_end = (new Date()).getTime();

                    var ds = dmp.diff_prettyHtml(d);
                    document.getElementById('outputdiv').innerHTML = ds + '<BR>Time: ' + (ms_end - ms_start) / 1000 + 's';  
                }                   
            });
        }
    </script>

It's of course the Send button that calls myFunction() at the bottom of my web page, once the user has entered a word. I have also verified that my web page, once the list has been generated, has the divs with "new1" and "old1" as ids (they are generated through my php code).

Any help would be really appreciated! I feel like I've tried everything!

Thank you! :)

  • 写回答

1条回答 默认 最新

  • dongxizhe9755 2013-07-19 20:05
    关注

    Highly recommend using jQuery or some other lib for this.

    var url = "contributions_old.php?user=" + $("#user").val() + "&wiki=" + $("#wiki").val();
    $.get(url);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 flask项目,怎么使用AJAX传数据库数据到echarts图表的data里,实现异步加载数据。
  • ¥15 本题的答案是不是有问题
  • ¥15 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 蓝桥杯单片机第十三届第一场,整点继电器吸合,5s后断开出现了问题
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?