Home  >  Article  >  Web Front-end  >  jQuery simple Ajax call example

jQuery simple Ajax call example

巴扎黑
巴扎黑Original
2017-06-30 14:18:591112browse

jQuery It’s really convenient. Let’s make a simple Ajax call:

Create a simple html file:


<!DOCTYPE HTML><html><head><script type="text/javascript" src="Public/js/jquery-easyui-1.3.1/jquery-1.8.2.min.js"></script><script type="text/javascript">
    $(function(){        //按钮单击时执行        $("#testAjax").click(function(){              //取Ajax返回结果
              //为了简单,这里简单地从文件中读取内容作为返回数据              htmlobj=$.ajax({url:"/Readme.txt",async:false});               //显示Ajax返回结果               $("#myp").html(htmlobj.responseText);
         });
    });</script>    </head>
    <body>
        <p id="myp"><h2>通过 AJAX 改变文本</h2></p>
        <button id="testAjax" type="button">Ajax改变内容</button>
    </body></html>

Okay, click the button to see the effect.

Of course, jQuery's Ajax call can control many items, and a simple call is demonstrated here.

Pay attention to your own jqueryreferencepath.

Okay, let’s do an example of calling the background:


<!DOCTYPE HTML><html><head><script type="text/javascript" src="Public/js/jquery-easyui-1.3.1/jquery-1.8.2.min.js"></script><script type="text/javascript">
    $(function(){        //按钮单击时执行        $("#testAjax").click(function(){              
              //Ajax调用处理
            var html = $.ajax({
               type: "POST",
               url: "test.php",
               data: "name=garfield&age=18",
               async: false

            }).responseText;
            $("#myp").html('<h2>'+html+'</h2>');
         });
    });</script>    </head>
    <body>
        <p id="myp"><h2>通过 AJAX 改变文本</h2></p>
        <button id="testAjax" type="button">Ajax改变内容</button>
    </body></html>

Backend code:


<?php    $msg='Hello,'.$_POST['name'].',your age is '.$_POST['age'].'!';    echo $msg;

Now you can get data from the background!

Of course, we can also call Ajax like this:


<!DOCTYPE HTML><html><head><script type="text/javascript" src="Public/js/jquery-easyui-1.3.1/jquery-1.8.2.min.js"></script><script type="text/javascript">
    $(function(){        //按钮单击时执行        $("#testAjax").click(function(){              
              //Ajax调用处理            $.ajax({
               type: "POST",
               url: "test.php",
               data: "name=garfield&age=18",
               success: function(data){
                        $("#myp").html('<h2>'+data+'</h2>');
                  }
            });
            
         });
    });</script>    </head>
    <body>
        <p id="myp"><h2>通过 AJAX 改变文本</h2></p>
        <button id="testAjax" type="button">Ajax改变内容</button>
    </body></html>

Note that the data parameter in

success: function(data)

can be changed to another name, such as success: function( msg), msg(data) is the returned data. This is the parameter of the callback function, not the data parameter in the Ajax call in

data: "name=garfield&age=18"

.

The above is the detailed content of jQuery simple Ajax call example. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn