Home  >  Article  >  Web Front-end  >  Call ajax in jQuery to achieve asynchronous implementation

Call ajax in jQuery to achieve asynchronous implementation

php中世界最好的语言
php中世界最好的语言Original
2018-04-25 16:41:011726browse

This time I will bring you the precautions for calling ajax in jQuery to achieve asynchronous implementation, and the precautions for calling ajax in jQuery to achieve asynchronous implementation. The following is a practical case, let's take a look.

<script type="text/javascript" language="javascript" src="JS/jquery-1[1].2.3.min.js"></script>
<script type="text/javascript" language="javascript">
$(document).ready(function(){  //Jquery 页面加载事件,当页面加载之后首先执行这个方法
    //第一种Ajax请求
  $.ajax({
    type:"GET",    //请求类型,有get,post等类型,和表单提交是一样的
    url:"Result.aspx", //请求处理的页面,就是说由那个页面捕获请求,同样这个路径可以换成ashx,一般处理程序
    data: "name=John&location=Boston", //传递参数,实在就是(Result.aspx?name=John&loaciton=Boston)
    success:function(msg){ //请求成功会由这个方法处理,其中请求成功返回值由msg接收
      $("#ajaxp").text(msg);
    }
  });
  //第二种请求
  $.get(               //用get请求方式,其实就是上一种的变体
    "Handler.ashx",   
    {name:"笨笨熊",sex:"女"}, //要传递的参数,解析为(Handler.ashx?name=笨笨熊&sex=女)
    function(msg){       //请求返回参数
      $("#p1").text(msg);
    }
  );
  //第三种请求
  $.post(               //这种请求的上一种是一样的,只是请求方式不同
    "Handler2.ashx",
    {name:"benben 笨笨熊",sex:"女"},
    function(msg){
      $("#p2").text(msg);
    }
  );
  //第四种请求
  $.getJSON(             //这种是用JSON 实现的,JSON是一种通用的数据格式
    "Handler3.ashx",  //路径
    {name:"aaa"},   //传递参数
    function (data){  //回调处理函数 返回的就是json数据格式,由data接收这串数据
      var str="";     //下面就是解析这些数据,具体接送的使用但不做考虑,设计内容很多,一时无法讲解
      str+="姓名:  "+data.name+"<br/>";
      str+="性别:  "+data.sex+"<br/>";
      str+="地址:  "+data.address+"<br/>";
      $("#p3").html(str);
    }
  );
});
</script>

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

Let Ajax achieve asynchronous refresh without using plug-ins

The use of JS based on ajax operation information

The above is the detailed content of Call ajax in jQuery to achieve asynchronous implementation. 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