javascript xmlhttp.open(url) 和使用 AJAX 调用 php 函数不起作用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14766375/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 22:31:42  来源:igfitidea点击:

xmlhttp.open(url) and Calling php function using AJAX not working?

phpjavascriptajax

提问by Mayur

Just beginner in php, I want to call php method using AJAX. I tried every thing but don't know what error is. Not getting any response from object xmlhttp.

刚开始接触 php,我想使用 AJAX 调用 php 方法。我尝试了所有事情,但不知道错误是什么。没有从对象 xmlhttp 得到任何响应。

Heres my java script code :

继承人我的java脚本代码:

function loadData(){
    var mID=ddItems;
    var method=2;
    var xmlhttp;
    if (window.XMLHttpRequest) {    
        xmlhttp = new XMLHttpRequest();
    }
    if (xmlhttp.readyState == 4 || xmlhttp.readyState == 0) {
        xmlhttp.open("GET", "../code/GetItemsInDD.class.php?id=" + mID + "&method=" + method, true); **// is this statement correct**

    xmlhttp.onreadystatechange = function(){
        if (xmlhttp.readyState==4 && xmlhttp.status==200) **//conditin is false,**
        {
            document.getElementById("ddItems").innerHTML=xmlhttp.responseText; 
        }
     }
    xmlhttp.send();
    }
}

My js file is on "projectname/javascript/script.js" and my php file is in "projectname/code/GetItemsInDD.class.php" dir.

我的 js 文件在“projectname/javascript/script.js”上,我的 php 文件在“projectname/code/GetItemsInDD.class.php”目录下。

回答by Salman

Why don't you use jQuery for making AJAX requests? Its as simple as this, include jQuery in your page

为什么不使用 jQuery 来发出 AJAX 请求?就这么简单,在你的页面中包含 jQuery

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>

and JS code,

和JS代码,

$.ajax({
  type: 'GET',
  url: '../code/GetItemsInDD.class.php?id=" + mID + "&method=" + method',
  success: function (data) {
     document.getElementById("ddItems").innerHTML = data; 
  }
});

This way, you don't need to check for the readyState and status thing

这样,您就不需要检查 readyState 和 status 的东西

jQuery follows object oriented approach for declaring XMLHttpRequestobjects, so you won't have to worry about creating multiple objects for making more than one AJAX requests.

jQuery 遵循面向对象的方法来声明XMLHttpRequest对象,因此您不必担心创建多个对象来发出多个 AJAX 请求。

回答by Shijin TR

     function loadData(){
           var xmlhttp;
           var mID=ddItems;
           var method=2;
              if (window.XMLHttpRequest)
              {// code for IE7+, Firefox, Chrome, Opera, Safari
               xmlhttp=new XMLHttpRequest();
              }
             else
             {// code for IE6, IE5
             xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
              }
             xmlhttp.onreadystatechange=function()
               {
            if (xmlhttp.readyState==4 && xmlhttp.status==200)
              {
            document.getElementById("ddItems").innerHTML=xmlhttp.responseText;
              }
           }
           xmlhttp.open("GET", "../code/GetItemsInDD.class.php?id=" + mID + "&method=" + method, true);
          xmlhttp.send();
          }

回答by Palak Tanejaa

I have made 2 desired changes to your code, try running it now. Make sure the URL is correct.

我已经对您的代码进行了 2 处所需的更改,现在尝试运行它。确保 URL 正确。

function loadData()
{
var mID=ddItems;

var method=2;

var xmlhttp;

if (window.XMLHttpRequest) {    
    xmlhttp = new XMLHttpRequest();
}

else    //For some versions of IE
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");


xmlhttp.onreadystatechange = function()
{
    if (xmlhttp.readyState==4 && xmlhttp.status==200) **//conditin is false,**
    {
        document.getElementById("ddItems").innerHTML=xmlhttp.responseText; 
    }
 }

xmlhttp.open("GET", "../code/GetItemsInDD.class.php?id=" + mID + "&method=" + method, true); **// is this statement correct**
xmlhttp.send();
}

}

}

Change 1 : You may be running the code in an older version of IE, where ActiveXObject is used.

更改 1:您可能在使用 ActiveXObject 的旧版 IE 中运行代码。

Change 2 : The open() method should not be called if the readyState changes ( as you have written it within the IF block), readyState changes only after the ajax call is initialized by the open() method and then send by the send() method.

变化 2:如果 readyState 发生变化(正如您在 IF 块中所写的那样),则不应调用 open() 方法,readyState 仅在 open() 方法初始化 ajax 调用然后由 send() 发送后才会发生变化) 方法。