在jquery中使用Json方式传递单个值

结果图:
在这里插入图片描述

一、Action类

public class TestActinon extends ActionSupport {
	public String method() {
		this.msg="pkd";
		this.message="成功返回单个值!!!";
		return SUCCESS;
		}
	public String method1() {
		return SUCCESS;
}
}

二、配置Struts

<struts>
	<constant name="struts.enable.DynamicMethodInvocation" value="true" />
    <package name="default" namespace="/"   extends="json-default">
     <global-allowed-methods>regex:.*</global-allowed-methods>
 		<action name="returnmessage" class="com.action.TestActinon">
 			<result type="json"/>
 		</action>
    </package>

</struts>

三、在html页面通过jqury传递获取单个值

<script type="text/javascript">
	$(document).ready(function(){
		使用json的方式返回单个值
		$("#returnMsg").click(function(){
			$.getJSON("returnmessage!method.action",function(data){
				
				alert(data);
				alert(data.msg);
				alert(data.message);
				$("#showdata").html("<font color='red'>"+data.msg+"<br>"+data.message+"</font>");
				})
			});

		//使用json的方式传固定值
		$("#returnMsg1").click(function(){
			$.getJSON("returnmessage!method1.action",
					{msg:"lisi",message:"json传固定值"},function(data){
				alert(data);
				alert(data.msg);
				alert(data.message);
				$("#showdata").html("<font color='red'>"+data.msg+"<br>"+data.message+"</font>");
				})
			});

		//使用json的方式传文本框中的变量值
		$("#returnMsg2").click(function(){
			$.getJSON("returnmessage!method1.action?",{msg:$("#userId").val(),
				message:$("#userpwd").val()},function(data){
				alert(data);
				alert(data.msg);
				alert(data.message);
				$("#showdata").html("<font color='red'>"+data.msg+"<br>"+data.message+"</font>");
				})
			});
		
		
		//使用web方式传文本框中的变量值
		$("#returnMsg3").click(function(){
			$.getJSON("returnmessage!method1.action?msg="+$("#userId").val()
					+"&message="+$("#userpwd").val(),function(data){
				alert(data);
				alert(data.msg);
				alert(data.message);
				$("#showdata").html("<font color='red'>"+data.msg+"<br>"+data.message+"</font>");
				})
			});
	




		
		});
</script>
</head>
<body>
<center>
		<input type="button" value="使用json的方式返回单个值" id="returnMsg"/>
		<input type="button" value="使用json的方式传固定值" id="returnMsg1"/>
		<input type="button" value="使用json的方式传文本框中的变量值" id="returnMsg2"/>
		<input type="button" value="使用web方式传文本框中的变量值" id="returnMsg3"/><br>
	<div id="showdata"></div>
	<form>
		用户名: <input type="text" id="userId" ><br>
		密    码:<input type="text" id="userpwd" >
		
	</form>
	</center>
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值