A simple example of get and post in jquery

  • 2020-03-30 01:35:42
  • OfStack

Example:

Test. The HTML
Page reference < The script type = "text/javascript" SRC = "jquery - 1.3.2. Min. Js" > < / script>

Contents are as follows:
< Div id = "divMsg" > Hello World! < / div>

Usage 1:(read the contents of the remote page to divMsg when the page loads)

$(" # divMsg "). The load (http://localhost:8012/t.php, {" resultType ":" HTML "});

The return type resultType can be as follows:

"XML "," HTML ", "script", "json", "jsonp", "text"

Usage 2:(click post data to return data)


<input type="button" id="bnajax" value="ajax" onclick="ajaxTest()" />
<script type="text/javascript" >
 function ajaxTest()
 {
  $.post("http://localhost:8012/t.php", { "txt": "123" },function(data)
  {
   $("#divMsg").html(data);
  }
  );
 }
</script>

Here is an excerpt from the network function:

The post method is as follows:


    function test(access_url, tipE){
        $.post(access_url,{  
            first: "test1", second: "test2" 
            }, function(data){ 
                    if(data.success){ 
                        $('#' + tipE).html(' Handle a successful '); 
                    }else{ 
                        $('#' + tipE).html(data.msg);    
                    } 
            },'json' 
       ) 
    }

If you want to use the get method, just replace post with get. That's easy!

The data value in this function is the value returned by the server and is in JSON format. Of course, you can use other types, such as text, XML, and so on.

The server returns the value in JSON format, such as: {success:true, MSG :" test successful "}


Related articles: