jsp中怎么调用action的返回json数据

2024-11-24 17:03:15
推荐回答(1个)
回答1:

用jquery 或者js   发送ajax 请求就可以满足你的需求,下面是给出的jquery 例子:

           GET提交
           
            //复杂json对象提交
            var person = {'per':"{ 'id': 1, 'name': '5itjob', 'sex': '男' }"};
            $.ajax({
                type: "get",
                url: "JsonObject.asmx/GetPersonByObject",
                data: person,
                dataType: 'json',
                contentType: 'application/json;charset=utf-8',
                success: function (data) {
                    if (data.d == "1") {
                        $("#hello").text("服务器接收成功!");
                    }
                    else {
                        $("#hello").text("服务器接收数据失败!");
                    }
                },
                error: function () {
                    $("#hello").text("程序运行出错!");
                }
            });
            
            POST提交
            var person = "{'per':\"{ 'id': 1, 'name': '5itjob', 'sex': '男' }\"}";
            $.ajax({
                type: "post",
                url: "JsonObject.asmx/GetPersonByObject",
                data: person,
                dataType: 'json',
                contentType: 'application/json;charset=utf-8',
                success: function (data) {
                    if (data.d == "1") {
                        $("#hello").text("服务器接收成功!");
                    }
                    else {
                        $("#hello").text("服务器接收数据失败!");
                    }
                },
                error: function () {
                    $("#hello").text("程序运行出错!");
                }
            });