这个例子中包含$.get $.post $.ajax $.load

先贴上struts.xml

		<action name="demoD" class="com.lan.action.json.DemoAction"
			method="stringFunction">
			<result type="json" name="success">
				<param name="root">callBackString</param>
			</result>
		</action>
再贴上action

package com.lan.action.json;

import com.opensymphony.xwork2.ActionSupport;

/**
 * Struts2-Json 返回一个字符串
 * 
 * @author lan
 * 
 */
public class DemoAction extends ActionSupport {

	private String callBackString;
	private String input;

	public String stringFunction() {
		//System.out.println("Enther the DemoAction StringFunction !");
		callBackString = "返回输入的字符串:" + input;
		return SUCCESS;
	}

	public String getCallBackString() {
		return callBackString;
	}

	public void setCallBackString(String callBackString) {
		this.callBackString = callBackString;
	}

	public String getInput() {
		return input;
	}

	public void setInput(String input) {
		this.input = input;
	}
}

最后贴上js

/*
 * ajax 字符串
 */
$("#button001").click(function() {
	$.ajax({
		url : "demo1/demoD.action?method=stringFunction",
		type : "post",
		data : "input=" + $("#input").val(),
		dataType : "JSON",
		success : function(data) {
			alert(data);
		}
	});
});
$("#button002").click(function() {
	$.post("demo1/demoD.action?method=stringFunction", {
		input : $("#input").val()
	}, function(data) {
		alert(data);
	});
});
$("#button003").click(function() {
	$.get("demo1/demoD.action?method=stringFunction", {
		input : $("#input").val()
	}, function(data) {
		alert(data);
	});
});
$(function() {
	$("#button001").load("demo1/demoD.action?method=stringFunction", {
		input : "load"
	}, function(data) {
		$("#load-string").html(data);
	});
});


posted on 2013-12-05 10:45  wyang0126  阅读(198)  评论(0编辑  收藏  举报