我如何在Yii2应用程序中使用此功能?
<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>
我想将 autoload.php 更改为控制器功能http://localhost/ta/backend/web/index.php?r=sms/auto
答案 0 :(得分:0)
您需要在控制器中创建该功能才能进行通话。
路径将类似于此controllerName / actionName
阅读本例
//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>
我建议使用JQuery而不是Javascript Vanilla,更简单明了。