ajax常用请求方式

1.JAVA

    @RequestMapping(value = "testAjax")
    @ResponseBody
    public Map<String, Object> testAjax
            (@RequestParam(value = "name", required = true) String name,
             @RequestParam(value = "eName", required = true) String eName,
             @RequestParam(value = "description", required = true) String description) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("name", name);
        map.put("eName", eName);
        map.put("description", description);
        return map;
    }

2.AJAX

参数的描述:

     1.url:必需规定把请求发送到哪个 URL。

       2.data:可选映射或字符串值。规定连同请求发送到服务器的数据。

       3.success(data, textStatus, jqXHR) 可选。请求成功时执行的回调函数。

       4.可选、规定预期的服务器响应的数据类型。默认执行智能判断(xml、json、script 或 html)。

       5.error:可选、请求错误的情况下的回执

       6.async : false、//默认为true 异步     

一.$.ajax()返回其创建的 XMLHttpRequest 对象。$.ajax() 只有一个参数:参数 key/value 对象,包含各配置及回调函数信息。详细参数选项见下。如果你指定了 dataType 选项,请确保服务器返回正确的 MIME 信息,(如 xml 返回 "text/xml")。

 

$.ajax({
            url:basePath+'WebTset/testAjax',
            type:'post',
            dataType:'json',
            data:{
                name:"测试中",
                eName:"test...",
                description:"哈哈哈",
            },success:function(r){
                $.messager.alert('success', r.name+"、"+ r.eName+"、"+ r.description)
            },error:function(){
                $.messager.alert('error', "网络异常")
            }
        });

二.通过远程 HTTP GET 请求载入信息。

 $.get(basePath + "WebTset/testAjax", {
            name: "测试中",
            eName: "test...",
            description: "哈哈哈"
        }, function (data, status) {
            console.log("data:" + data.name)
            console.log("status:" + status)
        },"json");

三.通过远程 HTTP POST 请求载入信息。

$.post(basePath+'WebTset/testAjax', {
            name: "测试中",
            eName: "test...",
            description: "哈哈哈"
        }, function (r) {
            $.messager.alert('success', r.name + "、" + r.eName + "、" + r.description)
        },"json

四.通过 HTTP GET 请求载入 JSON 数据。

$.getJSON(basePath+'WebTset/testAjax', {
            name: "测试中",
            eName: "test...",
            description: "哈哈哈"
        },function(r){
            $.messager.alert('success', r.name + "、" + r.eName + "、" + r.description)
        });

 

posted @ 2017-06-09 15:52  逍遥叹!!  阅读(330)  评论(0编辑  收藏  举报