A simple example of get and post in jquery

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


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)

Here is an excerpt from the network function:

The post method is as follows:

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

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: