weixin_33734785 2015-09-29 07:24 采纳率: 0%
浏览 33

Yii2 Ajax xmlhttp.open

How i can use this function in my Yii2 aplication?

<script type="text/javascript">
        function ajaxrunning()
        {
            if (window.XMLHttpRequest)
            {
                xmlhttp=new XMLHttpRequest();
            }
            else
            {
                xmlhttp =new ActiveXObject("Microsoft.XMLHTTP");
            }

            xmlhttp.open("GET","autoload.php");
            xmlhttp.send();
            setTimeout("ajaxrunning()", 5000); 
        }
</script>


And i want to change the autoload.php to be the controller function http://localhost/ta/backend/web/index.php?r=sms/auto

  • 写回答

1条回答 默认 最新

  • weixin_33693070 2018-03-21 14:32
    关注

    You need to create the function in the controller to you make the call .

    The path will be like this controllerName/actionName

    Read this example

          //PHP UserController.php
    
            function actionSomething(){
              //do something
              return 'aaaa';
            } 
    
      //JS code 
    
    <script type="text/javascript">
    function ajaxrunning() {
        if (window.XMLHttpRequest) {
            xmlhttp = new XMLHttpRequest();
        }
        else {
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange = function () {
            if (xmlhttp.readyState == XMLHttpRequest.DONE) {
                if (xmlhttp.status == 200) {
                    console.log(xmlhttp.responseText);// this write 'aaaa'.
                }
                else if (xmlhttp.status == 400) {
                    alert('There was an error 400');
                }
                else {
                    alert('something else other than 200 was returned');
                }
            }
        }
        xmlhttp.open("GET", "/user/something");
        xmlhttp.send();
        setTimeout("ajaxrunning()", 5000);
    }
    </script>
    

    I recomend to use JQuery and not Javascript Vanilla, is more easy and clear.

    评论

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置