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

jsp中怎么调用action的返回json数据,第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/jsoncharset=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/jsoncharset=utf-8',

                success: function (data) {

                    if (data.d == "1") {

                        $("#hello").text("服务器接收成功!")

                    }

                    else {

                        $("#hello").text("服务器接收数据失败!")

                    }

                },

                error: function () {

                    $("#hello").text("程序运行出错!")

                }

            })

后台:

public class pageAction extends ActionSupport{

private String username

private String password

private String cmd

public String execute(){

String result = ""

String message = ""

//创建流

PrintWriter out = null

//创建json对象

JSONObject json = new JSONObject()

cmd = ServletActionContext.getRequest().getParameter("cmd")

username = ServletActionContext.getRequest().getParameter("username")

password = ServletActionContext.getRequest().getParameter("password")

//System.out.println("username:"+username+",password:"+password)

if("admin".equals(username) &&"admin".equals(password)){

json.put("content", "true")

}else{

json.put("content", "输入的账号或密码有误!")

}

out.write(json.toString())

return SUCCESS

}

}

前台:

<script type="text/javascript">

function checkAnswer() {

//获得输入的账号和密码的值

var username= document.getElementById("username").value

var password= document.getElementById("password").value

dataStr = {

checkname : username,

checkpass : password

}

jQuery

.ajax( {

type : "POST",

url : "ajax/checkAnswer.action?temp=" + Math.random(),

data : dataStr,

dataType : "json",

success : function(root) {

if ("true" == root.content) {

jQuery.messager.alert(" *** 作提示", "登录成功!", "info")

} else {

jQuery.messager.alert(" *** 作提示", root.content, "info")

}

}})

}

</script>

试试吧,大体是这样的格式,可能还会有一些小错误,注意下就好!

还要注意下在Structs里配置时:

<action name="seekcardAction" class="seekcardAction" method="returnResult">

<result type="json" /> //注意返回类型

</action>


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/6642002.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-03-26
下一篇 2023-03-26

发表评论

登录后才能评论

评论列表(0条)

保存