使用AJAX发送和回显[关闭]

Sorry if my question has been asked before, i tryed to seach but not answers (OR didn't understand the answer).

I have a link, and i like to get the value using AJAX, here is an exemple with PHP

HOME

<a href="page.php?value=3">Go</a>

PAGE

 $getValue = $_GET['value'];
 echo $getValue;

Thanks

Use $.get() to perform ajax get

jQuery

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>

$(document).ready(function(){
    $('a[href="page.php?value=3"]').click(function(e){
        e.preventDefault()
        $.get("page.php",{value:3},function(data){
           alert(data);
        });
    });
});

</script>
<a href="page.php?value=3">Go</a>

page.php

<?php
 $getValue = $_GET['value'];
 echo $getValue;
?>
  1. Include jQuery library.
  2. Wrap your code within $(document).ready(function(){ }) handler, in order to bind even after dom element is loaded.
  3. Use preventDefault() method to prevent browser default action on the event.
  4. Use click() for listening click event.
  5. At last use $.get() for Ajax get.

Use jQuery ajax to do so

$.ajax({
  type: "GET",
  url: "page.php",
  data: { value: 3}
});

page.php

<?php
 $getValue = $_GET['value'];
 echo $getValue;
?>

With pure Javascript:

<script type="text/javascript">
function loadXMLDoc() {
    var xmlhttp;

    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) {
            // put the xmlhttp.responseText in your html element
            document.getElementById("myDiv").innerHTML = xmlhttp.responseText;
        }
    }

    xmlhttp.open("GET", "page.php?value=3", true);
    xmlhttp.send();
}
</script>

With jQuery:

$.ajax({
    url: "page.php",
    data: { value: 3 },
    context: document.body,
    success: function(){
      $(this).addClass("done");
    }
});

the default method in jquery is GET, but you can change this, for more jquery info see this page https://api.jquery.com/jQuery.ajax/