Php Yii2 ajax xmlhttp.open
如何在我的Yii2应用程序中使用此功能Php Yii2 ajax xmlhttp.open,php,ajax,yii2,autoload,sms-gateway,Php,Ajax,Yii2,Autoload,Sms Gateway,如何在我的Yii2应用程序中使用此功能 <script type="text/javascript"> function ajaxrunning() { if (window.XMLHttpRequest) { xmlhttp=new XMLHttpRequest(); } else {
<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>
函数ajaxrunning()
{
if(window.XMLHttpRequest)
{
xmlhttp=新的XMLHttpRequest();
}
其他的
{
xmlhttp=新的ActiveXObject(“Microsoft.xmlhttp”);
}
open(“GET”,“autoload.php”);
xmlhttp.send();
setTimeout(“ajaxrunning()”,5000);
}
我想将autoload.php更改为控制器函数
您需要在控制器中创建函数以进行调用 路径将类似于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>
//PHP UserController.PHP
函数actionSomething(){
//做点什么
返回“aaaa”;
}
//JS代码
函数ajaxrunning(){
if(window.XMLHttpRequest){
xmlhttp=新的XMLHttpRequest();
}
否则{
xmlhttp=新的ActiveXObject(“Microsoft.xmlhttp”);
}
xmlhttp.onreadystatechange=函数(){
if(xmlhttp.readyState==XMLHttpRequest.DONE){
if(xmlhttp.status==200){
console.log(xmlhttp.responseText);//此写入为'aaaa'。
}
else if(xmlhttp.status==400){
警报(“出现错误400”);
}
否则{
警报(“返回了200以外的内容”);
}
}
}
open(“GET”,“/user/something”);
xmlhttp.send();
setTimeout(“ajaxrunning()”,5000);
}
我建议使用JQuery,而不是Javascript,这样更简单、更清晰
我也在苦苦挣扎,找到了这个关于Ajax的Yi2实现的链接。可能对您有用: