当前位置: 移动技术网 > IT编程>开发语言>JavaScript > jquery请求servlet实现ajax异步请求的示例

jquery请求servlet实现ajax异步请求的示例

2017年12月12日  | 移动技术网IT编程  | 我要评论

ajax可以发送异步请求实现无刷新效果,但是使用javascript比较麻烦,就query提供了一些封装的方法 ,可以使得操作更为简单:

$.ajax()方法:

function sendrequest() {
    $.ajax({
      url: "hello",
      type: "get",
      datatype: "txt",
      data: "name=zhangsan",
      complete: function(result){
        alert(result.responsetext);
      }
    });
  }

$.get()方法:

function sendrequestbyget(){
    $.get("hello","name=lisi",function(result){
      alert(result);
    });
  }

$.post()方法:

function sendrequestbypost(){
    $.post("hello","name=wangwu",function(result){
      alert(result);
    });
  }

$.load()方法:

//load代码等效使用如下$get()方法
  /*
  $get(url, data, function(result){
        //将result数据填充到页面元素中
    $(“#h2”).html(result);  
  });
  */

  function load(){
    $("h2").load("hello","name=hahaha");
  }

以上异步请求的hello文件:

protected void dopost(httpservletrequest req, httpservletresponse resp)
      throws servletexception, ioexception {
    string name=req.getparameter("name");
    resp.getwriter().print(name);
  }

以上这篇jquery请求servlet实现ajax异步请求的示例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网