• Ajax请求数据的两种方式


    ajax 请求数据的两种方法,有需要的朋友可以参考下。

    实现ajax 异步访问网络的方法有两个。第一个是原始的方法,第二个是利用jquery包的

    原始的方法不用引入jquery包,只需在html中编写script 片段

    这里我演示的是一个传递参数查询的例子;

    varurl="expert_ZFTservlet?expert_name="+"曾攀";

    一.原始的方法

    <scripttype="text/javascript">

    function load(){

    var xmlhttp;

    if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera,Safari

    xmlhttp= newXMLHttpRequest();

    }else{// code for IE6, IE5

    xmlhttp= newActiveXObject("Microsoft.XMLHTTP");

    }

    xmlhttp.onreadystatechange= function(){

    if (xmlhttp.readyState ==4 && xmlhttp.status == 200) {//获得了请求数据

    var expertinfolist = xmlhttp.responseText;

    //发送请求数据到myDiv document.getElementById("myDiv").innerHTML=expertinfolist;

    }

    }

    var url="expert_ZFTservlet?expert_name="+"曾攀";

    xmlhttp.open("GET", url,true);

    xmlhttp.send();

    }

    </script>

    </head>

    <body>

    <divid="myDiv">

    </div>

    <buttontype="button"onclick="load()">ChangeContent</button>

    </body>

    二.利用jquery包的ajax请求

    在使用该方法前需要引入Jquery包

    <scriptsrc="js/jquery-1.5.1.min.js"type="text/javascript"></script>

    <script>

    $.ajax({

    type:'post',//方法类型

    url:" expert_ZFTservlet?expert_name="+"曾攀",//请求地址

    dataType:'json',//数据类型

    success:callback//请求成功处理函数

    });

    //返回函数

    function callback(data){

    alert(data); //获得请求返回对象;

    }

    </script>

    这个是我请求的servlet 的一些代码

    expertinfolist为我的查询结果,

    为一个list<Object>类型的对象

    request.setCharacterEncoding("utf-8");

    response.setCharacterEncoding("utf-8");

    PrintWriterout=response.getWriter();

    out.print(expertinfolist);

    out.flush();

    out.close();

  • 相关阅读:
    FPGA quartus开发中常见的错误处理
    verilog中wire与reg类型的区别
    VC++6.0中ClassView中类消失 解决方案[转自网络]
    C++ 和 MFC的学习
    最近单片机编程中的心得
    #ifdef __cplusplus extern "C" { #endif”的定义的含义
    ES6字符串拼接新方法-模板字符串表达式
    JavaScript事件参数对象event
    JavaScript offset家族
    详解JavaScript中的replace()函数
  • 原文地址:https://www.cnblogs.com/du-0210/p/8378736.html
Copyright © 2020-2023  润新知