简体   繁体   English

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

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

Just beginner in php, I want to call php method using AJAX. 只是php的初学者,我想使用AJAX调用php方法。 I tried every thing but don't know what error is. 我尝试了所有事情,但不知道是什么错误。 Not getting any response from object xmlhttp. 没有从对象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”目录中。

Why don't you use jQuery for making AJAX requests? 您为什么不使用jQuery发出AJAX请求? Its as simple as this, include jQuery in your page 如此简单,在页面中包含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 XMLHttpRequest objects, so you won't have to worry about creating multiple objects for making more than one AJAX requests. jQuery遵循面向对象的方法来声明XMLHttpRequest对象,因此您不必担心创建多个对象来发出多个AJAX请求。

     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();
          }

I have made 2 desired changes to your code, try running it now. 我已经对您的代码进行了2次所需的更改,请立即尝试运行它。 Make sure the URL is correct. 确保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()方法,仅在open()方法初始化ajax调用然后由send()发送后,readyState才更改) 方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM