ajax学习笔记3-jQuery实现ajax(大拇指向上)

2021-07-15 23:07

阅读:554

YPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

标签:label   根据   字符串   arch   jquer   select   request对象   ber   xhtml   

jQuery实现ajax:

jQuery本身提供了一个ajax方法,jQuery.ajax([settings])

type:类型,”POST”或”GET”(默认)

url:发送请求的地址

data:是一个对象,连同请求发送到服务器的数据

dataType:预期服务器返回的数据类型,如果不指定,jQuery将自动根据HTTP包MIME信息来智能判断,一般采用json格式,可以设置为“json”

success:是一个方法,请求成功后的回调函数。传入返回后的数据,以及包含成功代码的字符串。

error:是一个方法,请求失败时调用此函数。传入XMLHttpRequest对象。

实现代码:

Demo

 body,input,button,select,h1{

                                                                    font-size:26px;

                                                                    line-height:1.8;

 }

 

员工查询

 

员工创建



 

 



 

 

$(document).ready(function(){

  $("#search").click(function(){

     $.ajax({

                                                                      type:"GET",

                                                                      url:"service1.php?number="+$("#keyword").val(),

                                                                      dataType:"json",

                                                                      success:function(data){

                                                                                if(data.success){  //如果json对象的success为true,则返回msg

                                                                                                                                                       $("#searchResult").html(data.msg);

                                                                                     }else{

                                                                                        $("#searchResult").html("出现错误:"+data.msg);                                       

                                                                               }

                                                                            },

                                                                            error:function(jqXHR){

                                                                                    alert("发生错误:"+jqXHR.status);

                                                                       }

                                                                     

                                                                   });

  });

 

 

 $("#save").click(function(){

     $.ajax({

                                                                      type:"POST",

                                                                      url:"service1.php",

                                                                      dataType:"json",

                                                                      data:{

                                                                               name:$("#staffName").val(),

                                                                               number:$("#staffNumber").val(),

                                                                               sex:$("#staffSex").val(),

                                                                               job:$("#staffJob").val()

                                                                            },

                                                                      success:function(data){

                                                                                if(data.success){  //如果json对象的success为true,则返回msg

                                                                                                                                                       $("#createResult").html(data.msg);

                                                                                     }else{

                                                                                        $("#createResult").html("出现错误:"+data.msg);                                       

                                                                               }

                                                                            },

                                                                            error:function(jqXHR){

                                                                                    alert("发生错误:"+jqXHR.status);

                                                                       }

                                                                     

                                                                   });

  });

 

 });

 

jQuery可以自动添加Content-Type。

 

ajax学习笔记3-jQuery实现ajax(大拇指向上)

标签:label   根据   字符串   arch   jquer   select   request对象   ber   xhtml   

原文地址:http://www.cnblogs.com/haimengqingyuan/p/7071919.html

上一篇:【JSP】配置错误页面

下一篇:URL


评论


亲,登录后才可以留言!