How to load a PHP page into a div with jQuery and AJAX?

后端 未结 6 540
名媛妹妹
名媛妹妹 2021-02-03 13:58

I am trying to write a function that will call getproduct.php?id=xxx when clicked. I can get the innerHTML portion to appear, but how do I also call t

6条回答
  •  逝去的感伤
    2021-02-03 14:39

    There are many ways by which you can load a page into a division .

    The very method is

    var xmlhttp;
        if (window.XMLHttpRequest) {
            xmlhttp = new XMLHttpRequest();
        } else {
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
          }
    
    xmlhttp.onreadystatechange = function() {
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            document.getElementById('digital_download').innerHTML=xmlhttp.responseText;
        }
    }
       xmlhttp.open("GET", 'getproduct.php?id=' + id,true);
       xmlhttp.send();
    }
    

    this is a typical method with no external reference.

    If you go with reference then there are 5 ways to make a ajax call with jQuery


    • load(): Load a piece of html into a container DOM.
    • jQuery.getJSON(): Load a JSON with GET method.
    • jQuery.getScript(): Load a JavaScript.
    • jQuery.get(): Use this if you want to make a GET call and play extensively with the response.
    • jQuery.post(): Use this if you want to make a POST call and don’t want to load the response to some container DOM.
    • jQuery.ajax(): Use this if you need to do something when XHR fails, or you need to specify ajax options (e.g. cache: true) on the fly.

提交回复
热议问题